
(1)依賴包:
通過maven引入
<dependency>
<groupid>javax.validation</groupid>
<artifactid>validation-api</artifactid>
<version>1.1.0.final</version>
</dependency>
<dependency>
<groupid>org.hibernate</groupid>
<artifactid>hibernate-validator</artifactid>
<version>5.1.2.final</version>
(2)要驗證的實體類
import javax.validation.constraints.assertfalse;
import javax.validation.constraints.asserttrue;
import javax.validation.constraints.decimalmax;
import javax.validation.constraints.decimalmin;
import javax.validation.constraints.max;
import javax.validation.constraints.min;
import javax.validation.constraints.notnull;
import javax.validation.constraints.pattern;
public class person {
@notnull(message = "使用者名稱不能為空")
private string name;
@max(value = 100, message = "年齡不能大于100歲")
@min(value= 18 ,message= "必須年滿18歲!" )
private int age;
//必須是ture
@asserttrue(message = "bln4 must is true")
private boolean bln;
//必須是false
@assertfalse(message = "blnf must is falase")
private boolean blnf;
@decimalmax(value="100",message="decim最大值是100")
private int decimax;
@decimalmin(value="100",message="decim最小值是100")
private int decimin;
// @length(min=1,max=5,message="slen長度必須在1~5個字元之間")
private string slen;
@notnull(message = "身份證不能為空")
@pattern(regexp="^(\\d{18,18}|\\d{15,15}|(\\d{17,17}[x|x]))$", message="身份證格式錯誤")
private string idcard;
@notnull(message="密碼不能為空")
private string password;
@notnull(message="驗證密碼不能為空")
private string rpassword;
get/set方法
}
(3)建構controller如下
@controller
public class springvalidatortest {
@requestmapping("/validator/springtest")
public void springte(@valid person person,bindingresult result){
if(result.haserrors()){
list<objecterror> list = result.getallerrors();
for(objecterror error: list){
//system.out.println(error.getobjectname());
//system.out.println(error.getarguments()[0]);
system.out.println(error.getdefaultmessage());//驗證資訊
}
}
}
}
(4)使用場景:添加或送出修改時進行字段的校驗
注意:bindingresult一定要緊跟在實體類的後面,否則報錯:
type status report
message
description the request sent by the client was syntactically incorrect.
錯誤的代碼:
@requestmapping(value = "/add",method=requestmethod.post)
public string addsavenews(@valid rolelevel rolelevel, model model, bindingresult binding) {
if(binding.haserrors()){
model.addattribute(rolelevel);
return jspfolder+"/add";
savecommon(rolelevel, model);
return redirectviewall;
正确的代碼:
public string addsavenews(@valid rolelevel rolelevel, bindingresult binding, model model) {
官方資料
example 2.1. field-level constraints
<a></a>
when using field-level constraints field access strategy is used to access the value to be validated. this means the validation engine directly accesses the instance variable and does not invoke the property accessor method even if such an accessor exists.
constraints can be applied to fields of any access type (public, private etc.). constraints on static fields are not supported, though.
when validating byte code enhanced objects property level constraints should be used, because the byte code enhancing library won't be able to determine a field access via reflection.
example 2.2. property-level constraints
the property's getter method has to be annotated, not its setter. that way also read-only properties can be constrained which have no setter method.
when using property level constraints property access strategy is used to access the value to be validated, i.e. the validation engine accesses the state via the property accessor method.
it is recommended to stick either to field or property annotations within one class. it is not recommended to annotate a field and the accompanying getter method as this would cause the field to be validated twice.
example 2.3. class-level constraint
http://www.yunmasoft.com
http://blog.csdn.net/xpsharp/article/details/9366865