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
        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
 
     < 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
     < 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 >
目录
相关文章
|
XML 安全 前端开发
Spring Security—Spring MVC 整合
Spring Security—Spring MVC 整合
193 1
|
3天前
|
前端开发 Java Spring
探索Spring MVC:@Controller注解的全面解析
在Spring MVC框架中,`@Controller`注解是构建Web应用程序的基石之一。它不仅简化了控制器的定义,还提供了一种优雅的方式来处理HTTP请求。本文将全面解析`@Controller`注解,包括其定义、用法、以及在Spring MVC中的作用。
18 2
|
存储 前端开发 Java
Spring MVC中@SessionAttributes注解使用详解
Spring MVC中@SessionAttributes注解使用详解
155 0
|
7月前
|
JSON 前端开发 Java
|
7月前
|
前端开发 Java Spring
DWR3+spring mvc实现
DWR3+spring mvc实现
|
7月前
|
JSON 前端开发 Java
Spring MVC常用的注解有哪些
Spring MVC常用的注解有哪些
|
存储 前端开发 Java
|
JSON 前端开发 Java
3.4 Spring MVC注解
注解用在方法的参数列表上,用于读取Request请求的body区的数据,然后对数据进行解析绑定到到Controller方法的参数上如果在请求头中的Content-Type是application/json,需要在参数前面加上@RequestBody。注解用在方法的参数列表上,将请求参数绑定到控制器的方法参数上如果HTTP请求头中的Content-Type是x-www-form-urlencoded,需要在List和Map等参数前加上@RequestParam。前端传入的参数名,与name属性一致。
74 0
|
前端开发 Java Spring