通常情况下,数据验证都分为前台验证,后台验证。并且前台JS验证是肯定有的,那么其实验证的错误信息根本不必通过后台传过去,哪怕就是想国际化,前台JS也能够胜任。
如果前台验证足够了,那么如果还有不正确的信息传到后台去,极有可能是通过非法手段。那么我对这些信息也不必客气,直接拒接就行了,连错误信息都不必发。 基于这个思想,
后台验证可以省掉很多东西。
我们现在使用Hibernate的那一套验证,必须引入Hibernate-validator的jar包。到Maven Repository 去找就行了。如果还找不到,拿到百度云里找吧
现有POJO类:
import java.lang.reflect.Field; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.NotEmpty; public class People { private int id; @NotEmpty private String name; @Email private String address; private int age; public final int getId() { return id; } public final void setId(int id) { this.id = id; } public final String getName() { return name; } public final void setName(String name) { this.name = name; } public final String getAddress() { return address; } public final void setAddress(String address) { this.address = address; } public final int getAge() { return age; } public final void setAge(int age) { this.age = age; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + "]"; } public People set(String name, Object obj) { try { Field f = this.getClass().getDeclaredField(name); f.setAccessible(true); f.set(this, obj); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } return this; } }
可以看到在属性name上有@NotEmpty,在属性address上有@Email(当然这是假装的)。
在Controller中有方法:
@RequestMapping(value="/add",method=RequestMethod.POST) public String add(@Valid People p,BindingResult result, HttpServletRequest request) { if(result.hasErrors()) return "error"; this.peopleService.add(p); return "people/detail"; }
注意在参数p之前有注解@Valid,并且在p之后紧接着就是BindingResult result。BindingResult result一定要紧跟着People p。
这样如果有错误,通过代码 if(result.hasErrors()) return "error"; 直接返回到error界面,error界面甚至不必存在。。。
另外,这样还有好处,People中 age的类型是int。如果传参数是age是一个不可以转换为int的字符串怎么办?
哈哈,这时 result.hasErrors() 的返回值也是true,也就是还是会返回error界面。
补充一点:非空验证是用的最多的,你会看到有 @NotEmpty,@NotNull,@NotBlank 三个差不多的注解
1.@NotNull 用于任何引用类型,验证是不是null
2.@NotEmpty。 用于CharSequence,Collection, Map and Arrays,验证不是null并且CharSequence长度不是0,collection,Map,Array大小不是0
3.@NotBlank。用于CharSequence。 验证是不是null;如果不是null,那么trim之后验证长度是不是0
所有日常验证字符串时,还是使用@NotBlank吧
关于可以有哪些验证的注解,直接网上copy的。
| 
 注解  | 
 适用的数据类型  | 
 说明  | 
| 
 @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  | 
 
  | 
 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)  | 
| 
 @Range(min=最小值, max=最大值)  | 
 
  | 
 验证注解的元素值在最小值和最大值之间  | 
| 
 @NotBlank  | 
 
  | 
 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格  | 
| 
 @Length(min=下限, max=上限)  | 
 
  | 
 验证注解的元素值长度在min和max区间内  | 
| 
 | 
 
  | 
 验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式  | 
更多信息请参考官方文档:http://docs.jboss.org/hibernate/validator/4.3/reference/en-US/html/validator-usingvalidator.html
原文:http://www.cnblogs.com/formyjava/p/4589092.html