javascript
springmvc的服务端数据验证-尊龙游戏旗舰厅官网
导入hibernate validator的jar包
hibernate-validator-4.3.0.final.jar
jboss-logging-3.1.0.cr2.jar
validation-api-1.0.0.ga.jar
定义实体类:
public class student {
private string name;
private double score;
private string mobile;
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public double getscore() {
return score;
}
public void setscore(double score) {
this.score = score;
}
public string getmobile() {
return mobile;
}
public void setmobile(string mobile) {
this.mobile = mobile;
}
@override
public string tostring() {
return "student [name=" name ", score=" score ", mobile="
mobile "]";
}
}
定义index.jsp页面
index.jsp
定义controller
import java.util.list;
import org.springframework.stereotype.controller;
import org.springframework.validation.bindingresult;
import org.springframework.validation.fielderror;
import org.springframework.validation.objecterror;
import org.springframework.validation.annotation.validated;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.servlet.modelandview;
@controller
public class mycontroller {
@requestmapping("/test/some.do")
//@validated 表名student是使用了bean验证
//bindingresult是异常信息
public modelandview dosome(@validated student student , bindingresult br) {
modelandview mv = new modelandview();
//默认的成功页面
mv.setviewname("/show.jsp");
//异常的判读处理
list
//size() > 0 表名有异常
if(errors.size() > 0 ){
//获取指定属性的异常
fielderror nameerror = br.getfielderror("name");
fielderror scoreerror = br.getfielderror("score");
fielderror mobileerror = br.getfielderror("mobile");
// name属性验证失败了,产生了error
if( nameerror != null){
mv.addobject("namemsg", nameerror.getdefaultmessage());
}
if( scoreerror != null){
mv.addobject("scoremsg", scoreerror.getdefaultmessage());
}
if( mobileerror != null){
mv.addobject("mobilemsg", mobileerror.getdefaultmessage());
}
mv.setviewname("/index.jsp");
}
return mv;
}
}
/**
只要发生数据验证失败,则需要将页面重新跳转到index.jsp表单页面,让用户重写输入
bindingresult 接口中常用的方法
getallerrors() :获取到所有的异常信息,其返回值为list ,但若没有发生异常,则该list也被创建,只不过其size()为0,而非list为null
getfielderror() : 获取指定属性的异常信息
geterrorcount() : 获取所有异常的数量
getrawfieldvalue() : 获取到用户输入引发验证异常的原始值.
*/
定义show.jsp页面
show.jsp
定义springmvc配置文件
在实体属性上添加验证注解:
使用的验证器注解均为:javax.validation.constraints包中的类.在注解的message属性中,可以使用{属性名}的方式来引用指定的注解的属性值
import javax.validation.constraints.max;
import javax.validation.constraints.min;
import javax.validation.constraints.pattern;
import javax.validation.constraints.size;
import org.hibernate.validator.constraints.notempty;
public class student {
//验证的注解放在属性上,指定验证的规则
@notempty(message="姓名不能为空")
@size(min=3,max=6,message="姓名长度是{min}到{max}")
private string name;
@min(value=0,message="成绩不能小于0")
@max(value=100,message="成绩不能大于100")
private double score;
@notempty(message="手机号不能为空")
@pattern(regexp="^1[34578]\\d{9}$",message="手机号格式不正确")
private string mobile;
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public double getscore() {
return score;
}
public void setscore(double score) {
this.score = score;
}
public string getmobile() {
return mobile;
}
public void setmobile(string mobile) {
this.mobile = mobile;
}
@override
public string tostring() {
return "student [name=" name ", score=" score ", mobile="
mobile "]";
}
}
hibernate validator中常用的验证注解介绍:
1.@assertfalse : 验证注解的元素值时false
2.@asserttrue : 验证注解的元素值时true
3.@decimalmax(value=x) : 验证注解的元素值小于等于指定的十进制value值
4.decimalmin(value=x) : 验证注解的元素值大于等于指定的十进制value值
5.@degits(integer=整数位数,fraction=小数位数) : 验证注解的元素值的整数位树和小数位数上线
6.@futrue : 验证注解的元素值(日期类型) 比当前时间晚
7.@max(value=x) : 验证注解的元素值小于等于指定的value值
8.@min(value=x) 验证注解的元素值大于等于指定的value值
9.@notnull : 验证注解的元素值不是null
10.@null : 验证注解的元素值是null
11.@past : 验证注解的元素值(日期类型)比当前时间早
12.@pattern(regex=正则表达式) : 验证注解的元素值与指定的正则表达式匹配
13.@size(min=最小值,max=最大值) : 验证注解的元素值在min和max(包含)指定区间,如字符长度,集合大小
14.@valid : 验证关联的对象 ,如账户对象里有一个订单对象,指定验证订单对象
15.@notempty : 验证注解的元素值不为null且不为空(字符长度不为0,集合大小不为0)
16.@renge(min=最小值,max=最大值) 验证注解的元素值在最小值和最大值之间
17.@notblank : 验证注解的元素不为空(不为null,去除首位空格后长度为0),不同于@notempty,@notblank只应用于字符串且在比较时会去除字符串的空格
18.@length(min=下限,max=上线) 验证注解的元素值长度在min和max区间内
19.@email : 验证注解的元素值时emali,也可以通过正则表达式和flag指定自定义的email格式
注意:
@notnull, @notempty 和@notblank的区别
@notnull 任何对象的value不能为null
@notempty 集合对象的元素不为0,即集合不为空,也可以用于字符串不为null
@notblank 只能用于字符串不为null,并且字符串trim()以后length要大于0
转载于:https://www.cnblogs.com/zwjcom/p/11365522.html
总结
以上是尊龙游戏旗舰厅官网为你收集整理的springmvc的服务端数据验证-----hibernate validator的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: