本篇要點:
一、JSR303校驗框架
二、Hibernate Validator擴展注解類
三、配置和使用SpringMVC校驗框架
一、JSR303校驗框架
JSR 303 用於對Java Bean 中的字段的值進行驗證,使得驗證邏輯從業務代碼中脫離出來。是一個運行時的數據驗證框架,在驗證之后驗證的錯誤信息會被馬上返回。一般用於表單提交頁面(如用戶名必填、只能由數字字母組成等等)。
• @NotNull 注解元素必須是非空
• @Null 注解元素必須是空
• @Digits 驗證數字構成是否合法
• @Future 驗證是否在當前系統時間之后
• @Past 驗證是否在當前系統時間之前
• @Max 驗證值是否小於等於最大指定整數值
• @Min 驗證值是否大於等於最小指定整數值
• @Pattern 驗證字符串是否匹配指定的正則表達式
• @Size 驗證元素大小是否在指定范圍內
• @DecimalMax 驗證值是否小於等於最大指定小數值
• @DecimalMin 驗證值是否大於等於最小指定小數值
• @AssertTrue 被注釋的元素必須為true
• @AssertFalse 被注釋的元素必須為false
二、Hibernate Validator擴展注解類
• @Email 被注釋的元素必須是電子郵箱地址
• @Length 被注釋的字符串的大小必須在指定的范圍內
• @NotEmpty 被注釋的字符串的必須非空
• @Range 被注釋的元素必須在合適的范圍內
三、配置和使用SpringMVC校驗框架
1.如何配置SpringMVC校驗框架
1>.在pom.xml配置jar包依賴:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.3.0.Alpha1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency>
2>.在springmvc.xml文件中加入如下一句:
<mvc:annotation-driven />
3>.在JavaBean中定義校驗規則:

public class User implements Serializable { @NotEmpty(message="用戶名不能為空") private String userName; @Pattern(regexp="[0-9a-zA-Z]{6,30}",message="密碼是6-30個字符,必須是字母或數字組合") private String password; @Length(min=2,max=100,message="非法真實姓名") private String realName; @Email("請輸入正確郵箱地址") private String email; @NotNull(message="請填寫您的年齡") private Integer age; .....
4>.在控制器的方法輸入參數中用@Valid注解參數實體
@RequestMapping(value="doRegister",method = RequestMethod.POST) public String doRegister(@Valid User user,......
2.獲取校驗結果,使用BindingResult result輸出錯誤
@RequestMapping(value="doRegister",method = RequestMethod.POST) public String doRegister(@Valid User user,BindingResult result,ModelMap map){ // 如果入參有問題,返回注冊頁面 if (result.hasErrors()) { List<FieldError> errorList = result.getFieldErrors(); for(FieldError error : errorList){ System.out.println(error.getField() + "*" + error.getDefaultMessage()); map.put("ERR_" + error.getField(), error.getDefaultMessage()); } return "register"; } // 這里省略注冊代碼 return "registersuccess"; }
校驗結果保存在BindingResult或Errors對象中:
• 這兩個類都位於org.springframework.validation包中
• 需校驗的表單對象和其綁定結果對象或錯誤對象是成對出現的
• Errors接口提供了獲取錯誤信息的方法,如getErrorCount()獲取錯誤的數量, getFieldErrors(String field)得到成員屬性的校驗錯誤列表
• BindingResult接口擴展了Errors接口,以便可以使用Spring的org.springframeword.validation.Validator對數據進行校驗,同時獲取數據綁定結果對象的信息
3.在JSP頁面中顯示錯誤
<form action="doRegister.html" method="post"> <p> ${ERR_userName }<br> <span>用戶名:</span><input type="text" name="userName"> </p> <p> ${ERR_password }<br> <span>密 碼:</span><input type="password" name="password"> </p> <p> ${ERR_realName }<br> <span>真實姓名:</span><input type="text" name="realName"> </p> <p> ${ERR_email }<br> <span>郵 箱:</span><input type="text" name="email"> </p> <p> ${ERR_age }<br> <span>年 齡:</span><input type="text" name="age"> </p> <p> <input type="submit" value="注冊"> </p> </form>
4.自定義校驗規則(可寫在net.quickcodes.common中)
1>.定義接口Between.java;
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @Documented @Constraint(validatedBy = {BetweenValidator.class})//指定校驗實現類 public @interface Between { String message() default "年齡必須在{min}和{max}之間";//錯誤提示信息 Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default {}; int min();//添加的 int max();//添加的 }
2.>.實現接口BetweenValidator.java,需要實現ConstraintValidator接口:
public class BetweenValidator implements ConstraintValidator<Between, Integer>{ private int min ; private int max ; @Override public void initialize(Between annotation) {//初始化參數 min = annotation.min(); max = annotation.max(); } @Override public boolean isValid(Integer value, ConstraintValidatorContext context) {//校驗邏輯 if (value == null) { return false; } if (value >= min && value <= max) { return true; } return false; } }
3>.在實體字段上應用自定義校驗規則
@NotNull(message="請填寫您的年齡") @Between(min=18,max=45)//自定義校驗規則 private Integer age;
實現原理:
當User實體的字段上應用了自定義校驗規則Between接口時(同時給Between接口的min、max字段賦值),Between接口中指定了校驗實現類為BetweenValidator。BetweenValidator實現ConstraintValidator接口時將Between傳進去了,所以在BetweenValidator中就可以根據min和max來判斷了。
5.國際化錯誤信息
1>.在src/main/resouces下新建一個i18n的資源文件夾,並新建messages.properties配置文件:
Between.user.age=應該在{min}與{max}之間
Pattern.user.password=密碼是6-30個字符,必須是字母或數字組合
Length.user.realName=非法真實姓名
NotEmpty.user.userName=用戶名不能為空
Email.user.email=請輸入正確的郵件地址
NotNull.user.age=請填寫您的年齡
2>.新建messages_zh_CN.properties配置文件:
Between.user.age=應該在{min}與{max}之間 Pattern.user.password=密碼是6-30個字符,必須是字母或數字組合 Length.user.realName=非法真實姓名 NotEmpty.user.userName=用戶名不能為空 Email.user.email=請輸入正確的郵件地址 NotNull.user.age=請填寫您的年齡
3>.在/WEB-INF/springmvc.xml中將原來的
<mvc:annotation-driven />
改為:
<mvc:annotation-driven validator="validator" /> <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.ResourceBundleMessageSource" p:basename="i18n/messages" />
4>.將原來User實體中的錯誤提示信息改為國際化配置屬性即可:
public class User implements Serializable { @NotEmpty(message="{NotEmpty.user.userName}") private String userName ; @Pattern(regexp="[0-9a-zA-Z]{6,30}", message="{Pattern.user.password}") private String password ; @Length(min=2, max=100, message="{Length.user.realName}") private String realName ; @Email(message="{Email.user.email}") private String email ; @NotNull(message = "{NotNull.user.age}") @Between(min = 18, max = 45) private Integer age ; ........ }