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,如需转载请自行联系原作者
相关文章
|
2月前
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
9月前
|
前端开发 Java 测试技术
微服务——SpringBoot使用归纳——Spring Boot中的MVC支持——@RequestParam
本文介绍了 `@RequestParam` 注解的使用方法及其与 `@PathVariable` 的区别。`@RequestParam` 用于从请求中获取参数值(如 GET 请求的 URL 参数或 POST 请求的表单数据),而 `@PathVariable` 用于从 URL 模板中提取参数。文章通过示例代码详细说明了 `@RequestParam` 的常用属性,如 `required` 和 `defaultValue`,并展示了如何用实体类封装大量表单参数以简化处理流程。最后,结合 Postman 测试工具验证了接口的功能。
534 0
微服务——SpringBoot使用归纳——Spring Boot中的MVC支持——@RequestParam
|
9月前
|
JSON 前端开发 Java
微服务——SpringBoot使用归纳——Spring Boot中的MVC支持——@RequestBody
`@RequestBody` 是 Spring 框架中的注解,用于将 HTTP 请求体中的 JSON 数据自动映射为 Java 对象。例如,前端通过 POST 请求发送包含 `username` 和 `password` 的 JSON 数据,后端可通过带有 `@RequestBody` 注解的方法参数接收并处理。此注解适用于传递复杂对象的场景,简化了数据解析过程。与表单提交不同,它主要用于接收 JSON 格式的实体数据。
833 0
|
9月前
|
前端开发 Java 微服务
微服务——SpringBoot使用归纳——Spring Boot中的MVC支持——@PathVariable
`@PathVariable` 是 Spring Boot 中用于从 URL 中提取参数的注解,支持 RESTful 风格接口开发。例如,通过 `@GetMapping(&quot;/user/{id}&quot;)` 可以将 URL 中的 `{id}` 参数自动映射到方法参数中。若参数名不一致,可通过 `@PathVariable(&quot;自定义名&quot;)` 指定绑定关系。此外,还支持多参数占位符,如 `/user/{id}/{name}`,分别映射到方法中的多个参数。运行项目后,访问指定 URL 即可验证参数是否正确接收。
523 0
|
5月前
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
373 0
|
5月前
|
SQL Java 数据库连接
Spring、SpringMVC 与 MyBatis 核心知识点解析
我梳理的这些内容,涵盖了 Spring、SpringMVC 和 MyBatis 的核心知识点。 在 Spring 中,我了解到 IOC 是控制反转,把对象控制权交容器;DI 是依赖注入,有三种实现方式。Bean 有五种作用域,单例 bean 的线程安全问题及自动装配方式也清晰了。事务基于数据库和 AOP,有失效场景和七种传播行为。AOP 是面向切面编程,动态代理有 JDK 和 CGLIB 两种。 SpringMVC 的 11 步执行流程我烂熟于心,还有那些常用注解的用法。 MyBatis 里,#{} 和 ${} 的区别很关键,获取主键、处理字段与属性名不匹配的方法也掌握了。多表查询、动态
169 0
|
5月前
|
JSON 前端开发 Java
第05课:Spring Boot中的MVC支持
第05课:Spring Boot中的MVC支持
282 0
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
452 0
|
存储 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
345 0
|
开发框架 前端开发 .NET
[回馈]ASP.NET Core MVC开发实战之商城系统(三)
[回馈]ASP.NET Core MVC开发实战之商城系统(三)
263 0

热门文章

最新文章