Spring MVC data validation

Source: Internet
Author: User
Tags jboss

Spring MVC data validation

1. Add a jar (the jar will conflict with a version)

<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>

2. Configure Springmvc.xml

<!--configuring SPRINGMVC data Checksum
<mvc:annotation-driven validator= "Validator"/>
<bean id= "Validator" class= "Org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" >
<property name= "Providerclass" value= "Org.hibernate.validator.HibernateValidator" ></property>
</bean>

3. Test, annotated usage, (@Valid, Bindingresult, @pattern regexp) (3:29 minutes)

@RequestMapping (value = "/login", method = Requestmethod.post)
@ResponseBody
Public resultmessage Login (@Valid login Login,bindingresult errorresult) {
Boolean flag= errorresult.haserrors ();
String sb = "";
if (flag) {
List<fielderror> errorlist = Errorresult.getfielderrors ();
for (Fielderror fielderror:errorlist) {
String field = Fielderror.getfield ();
String Fielderrormessage=fielderror.getdefaultmessage ();
Sb=fielderrormessage+ ",";
}
Return Utils.renderresultmessageerror (SB);
}

@Valid Bean

Bindingresult Errorresult

/**
* @Eamil (message= "Sorry email input is wrong")
* @Pattern (regexp= "[\\u4e00-\\u9fa5]{4,10}", message= "name must be 4-10-bit kanji")
* @Max (value=100,message= "")
* @Min (value=1,message= "")
*/

/**
* Login Name
*/
@NotNull (message= "cannot be null")
@NotBlank (message= "cannot be a space")
@NotEmpty (message= "cannot be empty")
@Pattern (regexp= "[\\w{2,10}]", message= "entered the login name incorrectly 2-10 bit, please re-enter")
Private String loginId;

/**
* Password
*/
@NotNull (message= "cannot be null")
@NotBlank (message= "cannot be a space")
@NotEmpty (message= "cannot be empty")
@Pattern (regexp= "[\\w{3,6}]", message= "password must be a combination of 3-6 numbers or letters or underscores")
Private String Pass;

Spring MVC data validation

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.