Spring jsp 验证 form:errors标签
1 在model层添加验证规则
@NotNull @Size(min=2,max =30,message="姓名在2-30个字符之间") private String username;
2. 在model层添加验证规则
@PostMapping("/save") public String save(@Valid User user, BindingResult result, RedirectAttributes redirectAttributes,Model model) { if(result.hasErrors()){ user.setXb("女"); model.addAttribute("user",user); model.addAttribute("jinengItme",jineng()); model.addAttribute("guxiang",getGuxiang()); return "hello"; }
@Valid导入验证规则,BindingResult导入验证信息
result.hasErrors()遍历错误信息并输出
3.在视图层使用form:errors标签显示错误
<form:errors path="username" />