springMVC validator的配置

 1.引入依赖

     
      javax.validation
      validation-api
      1.1.0.Final
    

    
      org.hibernate
      hibernate-validator
      5.4.2.Final
    

 2.dispatcher-servelt.xml配置

  
        
        
        
    
    
    
        
        
            
                classpath:CustomValidationMessages
            
        
        
        
        
        
        
    

3.添加注释校检

springMVC validator的配置_第1张图片

4.在controller中使用

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(@Validated User user, BindingResult br){
        if (br.hasErrors()){
            return "user/add";
        }
        users.put(user.getUsername(),user);
        return "redirect:/user/users";
    }
}

5.jsp展示


    Username:
Password:
Age:
Sex:

 

你可能感兴趣的:(springMVC validator的配置)