Spring 3 MVC And JSR303 @Valid Example

简介:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package  com.xxx.training.controller;
 
import  com.xxx.training.model.User;
import  org.springframework.stereotype.Controller;
import  org.springframework.validation.BindingResult;
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RequestMethod;
import  org.springframework.web.servlet.ModelAndView;
 
import  javax.validation.Valid;
 
@Controller
@RequestMapping (value =  "/user" )
public  class  SignUpController {
     @RequestMapping (value =  "signup" , method = RequestMethod.POST)
     public  ModelAndView signUp( @Valid  User user, BindingResult result) {
         if  (result.hasErrors()) {
             return  new  ModelAndView( "signUpForm" );
         }
         return  new  ModelAndView( "done" );
     }
 
     @RequestMapping (method = RequestMethod.GET)
     public  ModelAndView displaySignUpForm() {
         return  new  ModelAndView( "signUpForm" "user" new  User());
     }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package  com.xxx.training.model;
 
 
import  org.hibernate.validator.constraints.NotEmpty;
import  org.hibernate.validator.constraints.Range;
 
public  class  User {
 
     @NotEmpty
     private  String name;
 
     @Range (min =  1 , max =  150 )
     private  int  age;
 
     public  String getName() {
         return  name;
     }
 
     public  void  setName(String name) {
         this .name = name;
     }
 
     public  int  getAge() {
         return  age;
     }
 
     public  void  setAge( int  age) {
         this .age = age;
     }
}

  message.properties

1
2
NotEmpty.user.name = Name is required!
Range.user.age = Age value must be between  1  and  150

  signUpForm.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<%@ taglib prefix= "form"  uri= "http://www.springframework.org/tags/form"  %>
<html>
<head>
     <style>
         .error {
             color: #ff0000;
         }
 
         .errorblock {
             color: # 000 ;
             background-color: #ffEEEE;
             border: 3px solid #ff0000;
             padding: 8px;
             margin: 16px;
         }
     </style>
</head>
 
<body>
<h2>Customer SignUp Form - JSR303  @Valid  example</h2>
 
<form:form method= "POST"  commandName= "user"  action= "signup" >
     <form:errors path= "*"  cssClass= "errorblock"  element= "div" />
     <table>
         <tr>
             <td>Customer Name :</td>
             <td><form:input path= "name" /></td>
             <td><form:errors path= "name"  cssClass= "error" /></td>
         </tr>
         <tr>
             <td>Customer Age :</td>
             <td><form:input path= "age" /></td>
             <td><form:errors path= "age"  cssClass= "error" /></td>
         </tr>
         <tr>
             <td colspan= "3" ><input type= "submit" /></td>
         </tr>
     </table>
</form:form>
 
</body>
</html>

  dispatchServlet-servlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
< beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 
     < context:component-scan  base-package="com.xxx.training"/>
 
     < bean  id="viewResolver"
           class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         < property  name="prefix">
             < value >/WEB-INF/pages/</ value >
         </ property >
         < property  name="suffix">
             < value >.jsp</ value >
         </ property >
     </ bean >
 
     <!-- support JSR303 annotation if JSR 303 validation present on classpath -->
     < mvc:annotation-driven  />
 
     < bean  class="org.springframework.context.support.ResourceBundleMessageSource"
           id="messageSource">
         < property  name="basename" value="messages" />
     </ bean >
 
</ beans >

  web。xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<? xml  version="1.0" encoding="UTF-8"?>
< web-app  version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     < welcome-file-list >
         < welcome-file >index.jsp</ welcome-file >
     </ welcome-file-list >
 
     < context-param >
         < param-name >contextConfigLocation</ param-name >
         < param-value >classpath:applicationContext.xml</ param-value >
     </ context-param >
 
     < listener >
         < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class >
     </ listener >
 
     < servlet-mapping >
         < servlet-name >default</ servlet-name >
         < url-pattern >*.png</ url-pattern >
     </ servlet-mapping >
     < servlet-mapping >
         < servlet-name >default</ servlet-name >
         < url-pattern >*.js</ url-pattern >
     </ servlet-mapping >
     < servlet-mapping >
         < servlet-name >default</ servlet-name >
         < url-pattern >*.css</ url-pattern >
     </ servlet-mapping >
 
     < servlet >
         < servlet-name >dispatcherServlet</ servlet-name >
         < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
         < load-on-startup >1</ load-on-startup >
     </ servlet >
 
     < servlet-mapping >
         < servlet-name >dispatcherServlet</ servlet-name >
         < url-pattern >/</ url-pattern >
     </ servlet-mapping >
 
     < filter >
         < filter-name >CharacterEncodingFilter</ filter-name >
         < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class >
         < init-param >
             < param-name >encoding</ param-name >
             < param-value >UTF-8</ param-value >
         </ init-param >
     </ filter >
     < filter-mapping >
         < filter-name >CharacterEncodingFilter</ filter-name >
         < url-pattern >/</ url-pattern >
     </ filter-mapping >
 
     < error-page >
         < error-code >404</ error-code >
         < location >/WEB-INF/pages/404.jsp</ location >
     </ error-page >
 
</ web-app >

  


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2836374.html,如需转载请自行联系原作者
相关文章
|
19天前
|
SQL Java 数据库连接
对Spring、SpringMVC、MyBatis框架的介绍与解释
Spring 框架提供了全面的基础设施支持,Spring MVC 专注于 Web 层的开发,而 MyBatis 则是一个高效的持久层框架。这三个框架结合使用,可以显著提升 Java 企业级应用的开发效率和质量。通过理解它们的核心特性和使用方法,开发者可以更好地构建和维护复杂的应用程序。
110 29
|
2月前
|
设计模式 前端开发 Java
步步深入SpringMvc DispatcherServlet源码掌握springmvc全流程原理
通过对 `DispatcherServlet`源码的深入剖析,我们了解了SpringMVC请求处理的全流程。`DispatcherServlet`作为前端控制器,负责请求的接收和分发,处理器映射和适配负责将请求分派到具体的处理器方法,视图解析器负责生成和渲染视图。理解这些核心组件及其交互原理,有助于开发者更好地使用和扩展SpringMVC框架。
69 4
|
3月前
|
前端开发 Java 开发者
Spring MVC中的请求映射:@RequestMapping注解深度解析
在Spring MVC框架中,`@RequestMapping`注解是实现请求映射的关键,它将HTTP请求映射到相应的处理器方法上。本文将深入探讨`@RequestMapping`注解的工作原理、使用方法以及最佳实践,为开发者提供一份详尽的技术干货。
234 2
|
4月前
|
JSON 前端开发 Java
SSM:SpringMVC
本文介绍了SpringMVC的依赖配置、请求参数处理、注解开发、JSON处理、拦截器、文件上传下载以及相关注意事项。首先,需要在`pom.xml`中添加必要的依赖,包括Servlet、JSTL、Spring Web MVC等。接着,在`web.xml`中配置DispatcherServlet,并设置Spring MVC的相关配置,如组件扫描、默认Servlet处理器等。然后,通过`@RequestMapping`等注解处理请求参数,使用`@ResponseBody`返回JSON数据。此外,还介绍了如何创建和配置拦截器、文件上传下载的功能,并强调了JSP文件的放置位置,避免404错误。
|
4月前
|
前端开发 Java 应用服务中间件
【Spring】Spring MVC的项目准备和连接建立
【Spring】Spring MVC的项目准备和连接建立
79 2
|
4月前
|
XML 前端开发 Java
Spring,SpringBoot和SpringMVC的关系以及区别 —— 超准确,可当面试题!!!也可供零基础学习
本文阐述了Spring、Spring Boot和Spring MVC的关系与区别,指出Spring是一个轻量级、一站式、模块化的应用程序开发框架,Spring MVC是Spring的一个子框架,专注于Web应用和网络接口开发,而Spring Boot则是对Spring的封装,用于简化Spring应用的开发。
324 0
Spring,SpringBoot和SpringMVC的关系以及区别 —— 超准确,可当面试题!!!也可供零基础学习
|
5月前
|
XML 缓存 前端开发
springMVC02,restful风格,请求转发和重定向
文章介绍了RESTful风格的基本概念和特点,并展示了如何使用SpringMVC实现RESTful风格的请求处理。同时,文章还讨论了SpringMVC中的请求转发和重定向的实现方式,并通过具体代码示例进行了说明。
springMVC02,restful风格,请求转发和重定向
|
6月前
|
开发框架 前端开发 .NET
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
85 0
|
9月前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
97 0
|
9月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
246 0