SpringMVC数据校验
2017-01-08 12:41
519 查看
一、数据校验
在web应用程序中,为了防止客户端传来的数据引发程序异常,常常需要对 数据进行验证。输入验证分为客户端验证与服务器端验证。客户端验证主要通过JavaScript脚本进行,而服务器端验证则主要通过Java代码进行验证。 为了保证数据的安全性,一般情况下,客户端和服务器端验证都是必须的
二、关键步骤:
①、导入JAR包
SpringMVC支持JSR(Java Specification Result,Java规范提案)303-Bean Validation数据验证规范。而该规范的实现者很多,其中较常用的是Hibernate Validator。需要注意的是,Hibernate Validator是与Hibernate ORM并列的Hibernate的产品之一。这一点从Hibernate官网上所提供的资源形式可以看出他们之间的关系。
②applicationContext.xml中配置验证器
③定义实体类,打注解标记
注:
下面是主要的验证注解及说明:
④书写Controller方法
⑤搭建jsp页面
实现效果:
若都不进行输入:
若输入合法:
在web应用程序中,为了防止客户端传来的数据引发程序异常,常常需要对 数据进行验证。输入验证分为客户端验证与服务器端验证。客户端验证主要通过JavaScript脚本进行,而服务器端验证则主要通过Java代码进行验证。 为了保证数据的安全性,一般情况下,客户端和服务器端验证都是必须的
二、关键步骤:
①、导入JAR包
SpringMVC支持JSR(Java Specification Result,Java规范提案)303-Bean Validation数据验证规范。而该规范的实现者很多,其中较常用的是Hibernate Validator。需要注意的是,Hibernate Validator是与Hibernate ORM并列的Hibernate的产品之一。这一点从Hibernate官网上所提供的资源形式可以看出他们之间的关系。
②applicationContext.xml中配置验证器
③定义实体类,打注解标记
public class UserInfo { @NotEmpty(message="用户名不能为空") @Size(min=3,max=6,message="姓名长度应在{min}-{max}") private String username; @NotNull(message="成绩最大值为100") @Min(value=0,message="成绩不能小于{value}") @Max(value=100,message="成绩不能大于{value}") private Integer score; @NotEmpty(message="手机号码不允许为空") @Pattern(regexp="^1[34578]\\d{9}$",message="手机号码格式不正确") private String phone; }
注:
下面是主要的验证注解及说明:
注解 | 适用的数据类型 | 说明 |
@AssertFalse | Boolean, boolean | 验证注解的元素值是false |
@AssertTrue | Boolean, boolean | 验证注解的元素值是true |
@DecimalMax(value=x) | BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence. | 验证注解的元素值小于等于@ DecimalMax指定的value值 |
@DecimalMin(value=x) | BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence. | 验证注解的元素值小于等于@ DecimalMin指定的value值 |
@Digits(integer=整数位数, fraction=小数位数) | BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence. | 验证注解的元素值的整数位数和小数位数上限 |
@Future | java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant. | 验证注解的元素值(日期类型)比当前时间晚 |
@Max(value=x) | BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type ofCharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number. | 验证注解的元素值小于等于@Max指定的value值 |
@Min(value=x) | BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the char sequence is evaluated), any sub-type of Number. | 验证注解的元素值大于等于@Min指定的value值 |
@NotNull | Any type | 验证注解的元素值不是null |
@Null | Any type | 验证注解的元素值是null |
@Past | java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant. | 验证注解的元素值(日期类型)比当前时间早 |
@Pattern(regex=正则表达式, flag=) | String. Additionally supported by HV: any sub-type of CharSequence. | 验证注解的元素值与指定的正则表达式匹配 |
@Size(min=最小值, max=最大值) | String, Collection, Map and arrays. Additionally supported by HV: any sub-type of CharSequence. | 验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小 |
@Valid | Any non-primitive type(引用类型) | 验证关联的对象,如账户对象里有一个订单对象,指定验证订单对象 |
@NotEmpty | CharSequence, Collection, Map and Arrays | 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0) |
@Range(min=最小值, max=最大值) | CharSequence, Collection, Map and Arrays,BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types | 验证注解的元素值在最小值和最大值之间 |
@NotBlank | CharSequence | 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格 |
@Length(min=下限, max=上限) | CharSequence | 验证注解的元素值长度在min和max区间内 |
@Email | CharSequence | 验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式 |
⑤搭建jsp页面
<form action="${pageContext.request.contextPath }/first.do" method="post"> <h1>数据验证</h1> 姓名:<input name="username"/>${namemsg }<br/><br/> 成绩:<input name="score" />${scoremsg}<br/><br/> 电话:<input name="phone" />${phonemsg }<br/><br/> <input type="submit" value="注册"/> </form>
实现效果:
若都不进行输入:
若输入合法:
相关文章推荐
- springmvc学习笔记(25)——数据格式化与数据校验
- springmvc的数据校验
- springmvc的数据校验
- springMvc数据校验
- springmvc的数据校验
- SpringMVC 和 Hibernate Validator的结合(初级)- 基于注解后端数据校验
- SpringMVC数据校验
- springMvc 数据绑定,类型转换,数据校验 解析
- 【SpringMVC学习06】SpringMVC中的数据校验
- springMVC数据校验
- SpringMVC 集成JSR303数据校验
- Springmvc数据校验
- SpringMvc的数据校验
- 使用SpringMVC数据验证框架validation做数据校验
- SpringMVC 数据校验
- SpringMVC 学习笔记(六) 数据绑定和JSR校验
- 【SpringMVC】SpringMVC系列12之数据类型转换、格式化、校验
- 学习SpringMVC(十八)之JSR303数据校验
- SpringMVC 数据校验
- SpringMVC的数据校验