Spring mvc adopts jsr303 bean validation framework

Source: Internet
Author: User

This is a specification that defines some elements for bean data validation. For example, your model has a user. java with an email in it. When a user registers, it needs to verify that the email is valid. The general approach is js front-end verification, but it is not secure. As a complete security solution, we must perform backend verification. Table 1. the built-in constraint Constraint details in Bean Validation @ Null the annotated element must be null @ NotNull the annotated element must not be null @ AssertTrue the annotated element must be true @ AssertFalse the annotated element must be false @ Min (value) the element to be commented must be a number and its value must be greater than or equal to the specified minimum value @ Max (value). The element to be commented must be a number, the value must be less than or equal to the specified maximum value @ DecimalMin (value) The annotated element must be a number and its value must be greater than or equal to the specified minimum value @ DecimalMax (value) the annotated element must be a number and its value must be less than or equal to the specified maximum value @ Size (max, min) the size of the element to be commented must be within the specified range @ Digits (integer, fraction) the element to be commented must be a number, The value must be within the acceptable range. @ Past the annotated element must be a previous date @ Future the annotated element must be a Future date @ Pattern (value) the annotated elements must comply with the specified Regular Expression hibernate implements and extends this specification; constraint details @ Email the annotated element must be the Email 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 of 2, jsr303 implements Hibernate Validator, which is a reference implementation of Bean validation. hibernate Validator provides all built-in constraint implementations in the JSR 303 specification, in addition to some additional constraint. This verification framework can be used independently, even if your architecture is spring mvc + ibatis. The following figure shows the benefits: 3. How to Use jsr303 validation in spring mvc 1) First, configure <mvc: annotation-driven/> 2 in spring Based on the annotion annotation driver) related jar packages, if 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> <groupId> org. springframework </groupId> <artifactId> spring-web </artifactId> <version >$ {spring. version }</version> </dependency> <groupId> org. springframework </groupId> <artifactId> spring-webmvc </artifactId> <v Ersion >$ {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 is not maven, come down hibernate-validator.jar to the project alone. 3) declare [java] @ NotEmpty (message = "name cannot be blank") private String name in model (some objects of data interaction, or vo; @ NotEmpty (message = "email cannot be blank") @ Email (message = "invalid email format") 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 spring va Result object returned after lidation verification // What do you want to do if verification fails? String code = result. getFieldError (). getCode (); // The type name of the verification error, such as NotEmpty result. getFieldError (). getdefamessage message (); // return m;} else {return null ;}</span> 5) if an error is displayed on the page, you can either use the el expression to obtain the model value or use the spring form label: <form: errors path = "*" cssClass = "errorblock" element = "div"/>, I am using spring mvc + mybatis. hibernate validation is an independent component and has little to do with orm. Ibatis is not implemented in the validation specification.

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.