Foreword: This article mainly introduces the related concepts and usage of data verification in the data binding process of SpringMVC .
This article focuses on the following issues:
- JSR303 validation framework
-
Spring's built-in validation constraint annotations
1. JSR303 verification framework
JSR 303 is Java's standard framework for bean data validation, and it has been included in JavaEE 6.0. JSR 303 specifies validation rules by annotating standard annotations such as @NotNull and @Max on bean properties, and validates beans through standard validation interfaces.
JSR303 validation framework annotation class:
- @NotNull annotation element must be non-null
- @Null annotation element must be empty
- @Digits verify that the digital composition is legal
- @Future verify if after current system time
- @Past verify if it is before the current system time
- @Max validates that the value is less than or equal to the maximum specified integer value
- @Min Verify that the value is greater than or equal to the minimum specified integer value
- @Pattern validates that the string matches the specified regular expression
- @Size validates that the element size is within the specified range
- @DecimalMax validates whether the value is less than or equal to the maximum specified decimal value
- @DecimalMin Verify that the value is greater than or equal to the minimum specified decimal value
- @AssertTrue the annotated element must be true
- @AssertFalse annotated elements must be false
Hibernate Validator is a reference implementation of JSR 303. In addition to supporting all standard validation annotations, it also supports the following extended annotations
HibernateValidator extension
- @Email the annotated element must be an email address
- The size of the @Length annotated string must be within the specified range
- @NotEmpty the annotated string must be non-empty
- The @Range annotated element must be in the appropriate range
2. SpringMVC data validation framework
Spring 4.0 has its own independent data verification framework and supports the JSR303 standard verification framework. When Spring performs data binding, it can call the verification framework at the same time to complete the data verification work. In Spring MVC, data validation can be performed directly through annotation-driven methods .
Spring itself does not provide the implementation of JSR303 , so the jar package of the implementer of JSR303 must be placed in the classpath. Hibernate Validator is a frequently used JSR303 verification framework .
<mvc:annotation-driven/> will install a LocalValidatorFactoryBean by default. By marking the @valid annotation on the input parameters of the processing method, Spring MVC can perform data validation after data binding. Mark a @Valid in front of the form/command object that has been marked with the JSR303 annotation. After the Spring MVC framework binds the request parameters to the input object, it will call the verification framework to perform verification according to the verification rules declared by the annotation. Spring MVC saves the verification result of the previous form/command object of the verification result through the stipulation of the processing method signature and saves it to the subsequent input parameter. The input parameter to save the verification result must be of type BindingResult or Errors .
What needs to be declared is that the Bean object to be verified and its binding result object or error object appear in pairs, and other input parameters are not allowed to be declared between them.
Mark the validation annotation in the properties of the form/command object class, add @Valid before the input parameter corresponding to the processing method, Spring MVC will implement the validation and save the validation result in the BindingResult or after the validation input object. Errors are included in the parameters.
Commonly used methods are:
- FieldError getFieldError(String field)
- List<FieldError> getFieldErrors()
- Object getFieldValue(String field)
- Int getErrorCount()
Additional instructions: Spring MVC 除了会将表单/命令对象的校验结果保存到对应的 BindingResult或 Errors 对象中外,还会将所有校验结果保存到“隐含模型”,即使处理方法的签名中没有对应于表单/命令对象的结果入参,校验结果也会保存在 “隐含对象” 中。隐含模型中的所有数据最终将通过HttpServletRequest的属性列表暴露给JSP 视图对象,因此在 JSP 中可以获取错误信息,在 JSP 页面上可通过 <form:errors path=“userName”> 显示错误消息。
3. 示例演示
在后台接收实体类中进行如下配置,详细代码可见附件:
@NotEmpty private String username; @Past private Date birth; @Email private String email;
如果配置了<mvc:annotation-driven/>,会默认支持上述校验注解。用起来比较简单,
测试结果如下: