步骤一:导入依赖

<!--数据效验-->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.4.1.Final</version>
</dependency>

步骤二:实体类

  

 

步骤三:新增控制器

@Controller
@RequestMapping("/person")
public class PersonController {

    @RequestMapping( value = "/validationPerson")
    //@ResponseBody
    public String validationPerson(@Validated Dog dog, BindingResult br, Model model){
        if (br.hasErrors()){
            //判断是否存在验证错误的信息
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError item:allErrors){
                System.out.println("1111111"+item.getDefaultMessage());
                model.addAttribute("error",item);
            }
        }
        return "error";
    }
}

步骤四:结果

  

 

 

 posted on 2019-11-12 14:15  wnwn  阅读(182)  评论(0编辑  收藏  举报