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