Spring MVC uses the jsr303 Bean Validation Validation Framework

Source: Internet
Author: User
Tags jboss

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.