Spring MVC data verification-validate annotation method, mvcvalidate
1. Description
Before learning the annotation method, you should first learn the encoding method spring injection. This helps you understand how the verification framework works. When an error occurs, the problem can be better solved. Therefore, this blog tutorial is based on the encoding method, just adding the annotation Method to the original basis.
2. configuration information
- Web. xml does not need to be changed
- Hello-servlet.xml will be the original loading method, changed to automatically add the default class with hibernate and Spring provided validate, the configuration is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: p = "http://www.springframework.org/schema/p" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemaLocation = "http://www.springframework.org/schema/beans http: // Www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <context: component-s Can base-package = "controller"> </context: component-scan> <bean class = "org. springframework. web. servlet. mvc. annotation. annotationMethodHandlerAdapter "/> <bean class =" org. springframework. web. servlet. view. internalResourceViewResolver "p: prefix ="/WEB-INF/jsp/"p: suffix = ". jsp "> <! -- <Property name = "viewClass" value = "org. springframework. web. servlet. view. JstlView"> </property> --> </bean> <! -- Load configuration based on validate --> <mvc: annotation-driven validator = "validator"/> <! -- <Bean id = "accountValidator" class = "dao. accountValidator "> </bean> --> <bean id =" validator "class =" org. springframework. validation. beanvalidation. localValidatorFactoryBean "> <property name =" providerClass "value =" org. hibernate. validator. hibernateValidator "/> <! -- If it is not added, the ValidationMessages under classpath is used by default. properties --> <property name = "validationMessageSource" ref = "messageSource"/> </bean> <bean id = "messageSource" class = "org. springframework. context. support. reloadableResourceBundleMessageSource "> <property name =" basename "value =" classpath: message "/> <property name =" fileEncodings "value =" UTF-8 "/> <property name =" cacheSeconds "value =" 120 "/> </bean> </beans>
- The loading of the new configuration file message. properties to the file under the src directory is determined by the value of the attribute basename in the field messageSource in the hello-servlet.xml. You do not need to configure this file, but it is generally used to support internationalization, but the information is written to the configuration file as follows:
account.username.size=\u7528\u6237\u540D\u7684\u957F\u5EA6\u57283-20\u4E2A\u5B57\u7B26\u4E32\u4E4B\u95F4account.username.space=\u7528\u6237\u540D\u5B57\u7B26\u4E32\u4E4B\u95F4\u4E0D\u80FD\u6709\u7A7A\u683Caccount.password.size=\u5BC6\u7801\u7684\u957F\u5EA6\u8303\u56F4\u57286-20\u4E2A\u5B57\u7B26\u4E32\u4E4B\u95F4
Garbled characters are generated after escaping. The corresponding Chinese characters are as follows:
3. Source Code
-Modify the Account class with the following code:
package dao;import javax.validation.constraints.Pattern;import javax.validation.constraints.Size;public class Account { @Size(min=3,max=20,message="{account.username.size}") @Pattern(regexp="^[a-zA-Z0-9]+$",message="{account.username.space}") private String username; @Size(min=6,max=20,message="{account.password.size}") private String password; public Account() { // TODO Auto-generated constructor stub } public Account(String username, String password) { super(); this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
@ Size @ Pattern is the data verification method in Annotation mode, and the message information is defined in message. properties.
- No need for the AccountValidate class
3. effect demonstration
Start the tomcat service and enter http: // localhost: 8080/annotation_springmvc/register in the browser.
We will test it with a string with spaces:
Registration results:
4. Code Location
If necessary, you can download it at this location to see: data verification in Annotation mode, and the jar package is also uploaded.