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 >
目录
相关文章
|
11月前
|
XML 安全 前端开发
Spring Security—Spring MVC 整合
Spring Security—Spring MVC 整合
153 1
|
XML 前端开发 Java
【Spring MVC 系列】Spring MVC 中 Filter 配置的 6 种方式,看看你了解哪些
Filter 简介 过滤器 Filter 在 Servlet 2.3 版本中被首次提出,唯一的作用就是过滤,它不仅可以过滤请求,还可以过滤响应,当请求到达 Servlet 容器,会先经过 Filter ,然后再交给 Servlet,之后 Filter 还可以对 Servlet 的响应进一步处理。并且多个 Filter 还能形成一个链。使用图示表达如下。
1051 0
【Spring MVC 系列】Spring MVC 中 Filter 配置的 6 种方式,看看你了解哪些
|
JSON 前端开发 Java
Spring MVC 中的常见注解的用法(中)
Spring MVC 中的常见注解的用法
90 0
|
前端开发 Java API
Spring MVC 中的常见注解的用法(上)
Spring MVC 中的常见注解的用法
105 0
|
前端开发 Java Spring
Spring MVC 中获取session的几种方法
Spring MVC 中获取session的几种方法
408 0
|
存储 前端开发 Java
Spring MVC request 获取方式大总结
前言 普通的 Java Web 项目中,我们经常使用 HttpServletRequest 获取请求参数,请求头等信息。 到了 Spring MVC 项目,我们通常会使用 Spring 提供的注解获取参数,如 @RequestParam、@RequestHeader。 不过在某些场景下,我们可能还是想获取 HttpServletRequest 对象,如获取请求 IP,获取请求域名等。这篇我们来学习如何在 Spring MVC 环境下获取 HttpServletRequest,以及它们的实现方式,以做到知其所以然。
565 0
Spring MVC request 获取方式大总结
|
Java Spring
Spring中的Spring JSR-250 注释之@Resource
Spring中的Spring JSR-250 注释之@Resource
77 0
Spring中的Spring JSR-250 注释之@Resource
|
前端开发 Java Spring
|
前端开发 Java Spring