spring mvc中的valid
当你希望在spring mvc中直接校验表单参数时,你可以采用如下操作:
声明Validator的方式:
1.为每一个Controller声明一个Validator
@Controller
public class MyController { @InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new FooValidator());
}
}
2.为所有的Controller声明一个Validator
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven validator="globalValidator"/>
</beans>
如果你希望在spring mvc中使用jsr303实现类校验,则需要把jsr303实现类,比如hibernate Validator,放到classpath中,spring会自动扫描进行校验.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven />
</beans>
如果你希望在实体类中主动使用这个校验类,如下操作:
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidator FactoryBean"/>
使用spring 依赖注入即可.
import javax.validation.Validator; @Service
public class MyService { @Autowired
private Validator validator; }
spring mvc中的valid的更多相关文章
- Spring MVC 中 @ModelAttribute 注解的妙用
		
Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...
 - Spring mvc中@RequestMapping 6个基本用法
		
Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: Java代码 @Reques ...
 - spring mvc中使用freemark的一点心得
		
参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...
 - Http请求中Content-Type讲解以及在Spring MVC中的应用
		
引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...
 - Spring mvc中@RequestMapping 6个基本用法小结(转载)
		
小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMapping(value="/departments" ...
 - Spring MVC中处理静态资源的多种方法
		
处理静态资源,我想这可能是框架搭建完成之后Web开发的”头等大事“了. 因为一个网站的显示肯定会依赖各种资源:脚本.图片等,那么问题来了,如何在页面中请求这些静态资源呢? 还记得Spring MVC中 ...
 - Spring MVC 中的基于注解的 Controller【转】
		
原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...
 - spring mvc中的文件上传
		
使用commons-fileupload上传文件所需要的架包有:commons-fileupload 和common-io两个架包支持,可以到Apache官网下砸. 在配置文件spring-mvc.x ...
 - spring mvc中的@PathVariable(转)
		
鸣谢:http://jackyrong.iteye.com/blog/2059307 ------------------------------------------------ spring m ...
 
随机推荐
- android 单词
			
inflate: 胀, 膨, 通货膨胀, 膨胀系数
 - 51nod1199 Money out of Thin Air
			
链剖即可.其实就是利用了链剖后子树都在一段连续的区间内所以可以做到O(logn)查询和修改. 线段树细节打错了..要专心!肉眼差错都能找出一堆出来显然是不行的!. #include<cstdio ...
 - 一个小面试题sql
			
一. 问答题 1简要说明分页是如何实现的. A:sqlserver: Select top(pagesize) * from student where id not in( ...
 - 移植yaffs文件系统
			
需要下载yaffs2-d43e901.tar.gz,busybox-1.13.0.tar.bz2 使用的交叉编译器是4.33 1.修改配置编译busybox 修改Makefile CROSS_COMP ...
 - Linux/Android 性能优化工具 perf
			
/***************************************************************************** * Linux/Android 性能优化工 ...
 - 20160122.CCPP详解体系(0001天)
			
程序片段(01):Hello.c 内容概要:HelloWorld //01.#include表示包含的作用: // (1).<>:表示只在系统目录之下进行查找 // (2)."& ...
 - (六)6.15 Neurons Networks Deep Belief Networks
			
Hintion老爷子在06年的science上的论文里阐述了 RBMs 可以堆叠起来并且通过逐层贪婪的方式来训练,这种网络被称作Deep Belife Networks(DBN),DBN是一种可以学习 ...
 - 隐藏 php apache 的版本号
			
隐藏Apache版本信息 找到apache安装路径,譬如\Apache\conf\httpd.conf 修改httpd.conf ServerTokens ProductOnly ServerSign ...
 - concat、reverse面试题
			
1.concat数组连接 ,,]; ,,]; ,,]; alert(arr3.concat(arr1,arr2)); 结果:9,9,9,2,3,4,5,6,7 2.reverse将数组内容颠个个 ,, ...
 - poj 3254(状态压缩基础题)
			
题意:就是你给一个n行m列的矩阵,矩阵里的元素由0和1组成,1代表肥沃的土地可以种植作物,0则不可以种植作物,并且相邻的土地不能同时种植作物,问你有多少种种植方案. 分析:这是我做的第一道状态压缩dp ...