springMVC validator

1. Guide package

    The packages imported by the validator are: hibernate-validator-4.3.0.Final.jar, validation-api-1.0.0.GA.jar, jboss-logging-3.1.0.CR2.jar

 

2. Configure in the springmvc.xml configuration file

   2.1. Configure the validator

        <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">

                  <!-- Specify the configured validator provider class as HibernateValidator -->

                <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> 

                <!--Corresponding error message-->

                <property name="validationMessageSource" ref="messageSource"/>

       </bean>

    2.2. Configuration error message configuration file

       <!-- The class of the resource file defined by the loading error message is org.springframework.context.support.ReloadableResourceBundleMessageSource-->

      <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">     

           <!-- Specify the resource file name as basenames -->        

           <property name="basenames">
                <list>

                    <!--Add resource file configuration information-->
                    <value>classpath:ProductValidationMessages</value>
                </list>
            </property>

            <!--Specify the file encoding as utf-8-->
            <property name="fileEncodings" value="utf-8"/>

             <!--Specify the content cache time as 120s-->
            <property name="cacheSeconds" value="120"/>

      </bean>

    2.3. Add a validator for the processor adapter

       <mvc:annotation-driven validator="validator"></mvc:annotation-driven>

 

3. Create the ProductValidationMessages.properties configuration file

       user.name.length.error=Please enter a username of 1-10 characters
       user.password.input.error=Please enter the correct password

 

4. Add verification annotation information

   public class User {         

          @Size(min=1,max=5,message="{user.name.length.error}" )   

          private String username;

          @NotEmpty(message="{user.password.input.error}")
          private String 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;
         }
        @Override
         public String toString() {
             return "User [username=" + username + ", password=" + password + "]";
        }
    }

5. Add a validator to the Controller class

@Controller
public class UserController {

    @RequestMapping("userLogin.action")
    public String userLogin(Model model,@Validated(value=UserGroup2.class) User user,BindingResult bindingResult){
       List<ObjectError> allErrors = null;

        if(bindingResult.hasErrors()){
             allErrors = bindingResult.getAllErrors();
             for(ObjectError error:allErrors){
                  System.out.println(error.getDefaultMessage());
              }
            return "index";
          }

         System.out.println(user);
         model.addAttribute("user", user);
          return "user";
    }
    }

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324721803&siteId=291194637