JSR303 Verification--hibernate-validator Implementation

Source: Internet
Author: User

This article is a Spring+springmvc+mybatis project, but the JSR303 checksum uses the implementation provided by Hibernate.

1. Join MAVEN

<!--JSR303 Data validation Support: TOMCAT7 and above Servers -  <Dependency>      <groupId>Org.hibernate</groupId>      <Artifactid>Hibernate-validator</Artifactid>      <version>5.4.1.Final</version>  </Dependency>  

2. Add a validation rule to the entity class to validate

 Public class Employee {      private  Integer empId;      @Pattern (RegExp= ("(^[a-za-z0-9_-]{6,16}$) | ( ^[\\u2e80-\\u9fff]{2,5}$), message= "User name must be legal")      private  String empname      ; Private String gender;      @Pattern (RegExp= ("^ ([a-z0-9_\\.-]+) @ ([\\da-z\\.-]+) \ \. [A-z\\.] {2,6}) $ "), message=" The mailbox format must be legal! " )      private  String email;       Private Integer did;  }  

3. Add the @valid annotation and the "Bindingresult result" method to receive the validation results and return the validation results to the front-end display.

/*** Employee Save * 1. Support JSR303 Check * 2. Import Hibernate-validator*/@RequestMapping (Value= "/emp", method=requestmethod.post) @ResponseBody PublicMsg saveemp (@Valid Employee employee,bindingresult result) {if(Result.haserrors ()) {//validation failed, return failed, display failed error message in modal boxlist<fielderror> errors =result.getfielderrors (); Map<string, object> map =NewHashmap<string, object>();  for(Fielderror fielderror:errors) {System.out.println ("Wrong field name:" +Fielderror.getfield ()); System.out.println ("Error message:" +fielderror.getdefaultmessage ());          Map.put (Fielderror.getfield (), Fielderror.getdefaultmessage ()); }          returnMsg.fail (). Add ("Errorfield", map); }Else{employeeservice.saveemp (employee); returnmsg.success (); }  }

4. Front-end display

//2. Save$.ajax ({URL:"${ctx}/emp", type:"POST", data:$ ("#empAddModal form"). Serialize (), Success:function(data) {if(data.code==100){              //1. Close the modal frame$ ("#empAddModal"). Modal ("Hide"); //2. Come to the last pageTo_page (9999); }Else{              //Display failure Information            //Which field is displayed with the error message of the field            if(Undefined! =data.extend.errorField.email) {                  //Display mailbox error messagesShow_validate_msg ("#email_add_input", "Error", Data.extend.errorField.email); }              if(Undefined! =data.extend.errorField.empName) {Show_validate_msg ("#empName_add_input", "Error", Data.extend.errorField.empName); }}}, error:function(data) {}});

JSR303 Verification--hibernate-validator Implementation

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.