<!-- enables the spring mvc @controller programming model --> <mvc:annotation-driven validator="validator" /> <!-- 配置数据校验 --> <bean id="messagesource" class="org.springframework.context.support.reloadableresourcebundlemessagesource"> <property name="baname" value="classpath:messages"/> <property name="fileencodings" value="utf-8"/> <property name="cacheconds" value="10"/> </bean> <bean id="validator" class="org.springframework.validation.beanvalidation.localvalidatorfactorybean"> <property name="providerclass" value="org.hibernate.validator.hibernatevalidator"/> 非主流伤感 <property name="validationmessagesource" ref="messagesource"/> </bean>
(摘自hibernate validator reference):
验证注解
验证的数据类型
说明
@asrtfal
boolean,boolean
验证注解的元素值是fal
@asrttrue
boolean,boolean
验证注解的元素值是true
@notnull
任意类型
验证注解的元素值不是null
@null
任意类型
验证注解的元素值是null
@min(value=值)
bigdecimal,biginteger, byte,
short, int, long,等任何number或charquence(存储的是数字)子类型
验证注解的元素值大于等于@min指定的value值
@max(value=值)
和@min要求一样
验证注解的元素值小于等于@max指定的value值
@decimalmin(value=值)
和@min要求一样
验证注解的元素值大于等于@ decimalmin指定的value值
@decimalmax(value=值)
和@min要求一样
验证注解的元素值小于等于@ decimalmax指定的value值
@digits(integer=整数位数, fraction=小数位数)
和@min要求一样
验证注解的元素值的整数位数和小数位数上限
@size(min=下限, max=上限)
字符串、collection、map、数组等
验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小
@past
java.util.date,
java.util.calendar;
joda time类库的日期类型
验证注解的元素值(日期类型)比当前时间早
@future
与@past波罗密多心经要求一样
验证注解的元素值(日期类型)比当前时间晚
@notblank
charquence子类型
验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@notempty,@notblank只应用于字符串且在比较时会去除字符串的首位空格
@length(min=下限, max=上限)
charqueunit8nce子类型
验证注解的元素值长度在min和max区间内
@notempty
charquence子类型、collection、map、数组
验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)
@range(min=最小值, max=最大值)
bigdecimal,biginteger,charquence, byte, short, int, long等原子类型和包装类型
验证注解的元素值在最小值和最大值之间
@email(regexp=正则表达式,
flag=标志的模式)
charquence子类型(如string)
验证注解的元素值是email,也可以通过regexp和flag指定自定义的email格式
@pattern(regexp=正则表达式,
flag=标志的模式)
string,任何charquence的子类型
验证注解的元素值与指定的正则表达式匹配
@valid
任何非原子类型
指定递归验证关联的对象;
如用户对象中有个地址对象属性,如果想在验证用户对象时一起验证地址对象的话,在地址对象上加@valid注解即可级联验证
<!-- 校验器 --> <bean id="validator" class="org.springframework.validation.beanvalidation.localvalidatorfactorybean"> <!-- 校验器--> <property name="providerclass" value="org.hibernate.validator.hibernatevalidator" /> <!-- 指定校验使用的资源文件,如果不指定则默认使用classpath下的validationmessages.properties --> <property name="validationmessagesource" ref="messagesource" /> </bean> <!-- 校验错误信息配置文件 --> <bean id="messagesource" class="org.springframework.context.support.reloadableresourcebundlemessagesource"> <!-- 资源文件名--> <property name="banames"> <list> <value>classpath:customvalidationmessages</value> </list> </property> <!-- 资源文件编码格式 --> <property name="fileencodings" value="utf-8" /> <!-- 对资源文件内容缓存时间,单位秒 --> <property name="cacheconds" value="120" /> </bean>
<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
package acm.ur.po; import javax.validation.constraints.notnull;i品位生活mport javax.validation.constraints.size; public class ur { private integer id; //校验名字1到30个字符中间 //message是提示校验出错信息 @size(min=1, max=30, message="{ur.name.length.error}") private string name; //非空校验 @notnull(message="{ur.num.nonull}") private string num; private string x; private string tel; . . . . public integer getid() { return id; } public void tid(integer id) { this.id = id; } }
配置文件中的代码
ur.name.length.error=请输入1到30个字符的名字ur.num.nonull=请输入商品的生产日期
@requestmapping(value = "updateur")public string updateur(@validated ur ur, bindingresult bindingresult){ list<objecterror> allerrors = bindingresult.getallerrors(); //获得错误信息 for(objecterror e : allerrors){ //输出错误信息 system.out.println(e.getdefaultmessage()); } try{ int count = urrvice.updateur(ur); } catch(exception e){ e.printstacktrace(); } return "message";}
每个controller方法中定义不同的校验分组
定义一个接口,里面可以不写东西,用来装一个分组
//校验名字1到30个字符中间//message是提示校验出错信息//标识此校验属于哪个分组,group也可属于多个分组@size(min=1, max=30, message="{ur.name.length.error}", groups={validation1.class})private string name;
@validated中的参数指向那个检验分组@requestmapping(value = "updateur")public string updateur(@validated(value={validation1.class}) ur ur, bindingresult bindingresult){ if(bindingresult.harrors()){ list<objecterror> allerrors = bindingresult.getallerrors(); //获得错误信 for(objecterror e : allerrors){ //输出错误信息 system.out.println(e.getdefaultmessage()); } } try{ int count = urrvice.updateur(ur); } catch(exception e){ e.printstacktrace(); } return "message";}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 00:10:28,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/798477092791a44ea35fc262cb4df111.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:使用Spring注入Hibernate验证框架.doc
本文 PDF 下载地址:使用Spring注入Hibernate验证框架.pdf
留言与评论(共有 0 条评论) |