This is a specification that defines elements for the data validation of the bean, such as your model has a user.java, there is an email, when the user registration to verify that the email is legitimate. The general practice is JS front-end verification, but not secure, as a complete security solution, we have to back-end verification.
Table 1. Built-in constraint in Bean Validation
Constraint More information
@Null The annotated element must be Null
@NotNull Annotated element must not be null
@AssertTrue The annotated element must be true
@AssertFalse The annotated element must be false
@Min (value) The annotated element must be a number whose value must be greater than or equal to the specified minimum value
@Max (value) The annotated element must be a number whose value must be less than or equal to the specified maximum value
@DecimalMin (value) The annotated element must be a number whose value must be greater than or equal to the specified minimum value
@DecimalMax (value) The annotated element must be a number whose value must be less than or equal to the specified maximum value
@Size (max, min) the size of the annotated element must be within the specified range
@Digits (integer, fraction) The annotated element must be a number whose value must be within an acceptable range
@Past The annotated element must be a past date
@Future The annotated element must be a future date
@Pattern (value) The annotated element must conform to the specified regular expression hibernate has implemented and extended the specification;
Constraint More information
@Email The annotated element must be an e-mail address
@Length The size of the annotated string must be within the specified range
@NotEmpty The annotated string must be non-empty
@Range The annotated element must be within the appropriate range
2,jsr303 implementation
At present, the implementation of a better Validation has Hibernate Validator, which is the reference implementation of Bean Validation. Hibernate Validator provides the implementation of all the built-in constraint in the JSR 303 specification, in addition to some additional constraint. Can be used independently, even if your schema is spring MVC + Ibatis can also be used alone with this validation framework. The benefits look below to know:
How to use jsr303 in 3,spring MVC validation
1) First configure the spring configuration based on the annotion annotation driver
<mvc:annotation-driven/>
2) related jar package, if using Maven
[HTML]
<repositories>
<repository>
<id>jboss repository</id>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!--Spring 3 dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Hibernate Validator--
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
</dependencies>
If it's not maven, come down and Hibernate-validator.jar the project alone.
3) declared in model (some object of data interaction, or VO)
[Java]
@NotEmpty (message= "name cannot be empty")
private String name;
@NotEmpty (message= "Email cannot be empty")
@Email (message= "Email format is not valid")
Private String Email;
[Java]
4) Use @valid and Bindingresult in the controller:
[Java] View plaincopy
<span style= "FONT-SIZE:18PX;" > @RequestMapping (value= "/test")
@ResponseBody
Private user test (@Valid user u,bindingresultresult) {
if (Result.haserrors ()) {//This is the result object returned by spring validation checksum
If the checksum fails, what do you want to do?
String code = Result.getfielderror (). GetCode ();//Verify the type name of the error such as Notempty
Result.getfielderror (). Getdefaultmessage ());//Error message
return m;
}else{
return null;
}
}</span>
5) on the page if you need to display an error, either by using the El expression to take the model median, or you can use the Spring form label:
<form:errors path= "*" cssclass= "errorblock" element= "div"/>
Finally, I am using spring mvc+ MyBatis, hibernate validation is a separate piece and ORM does not have much relationship. Ibatis is not implemented in the validation specification.
Spring MVC uses the jsr303 Bean Validation Validation Framework