- Spring Web - Spring Boot Starter Validation public class User { private String name; private int age; } @Controller public class UserController { @RequestMapping(value = "/user", method = RequestMethod.POST) public String validateUser(@Valid User user, BindingResult bindingResult, Model model) { if (bindingResult.hasErrors()) { return "error"; } else { model.addAttribute("name", user.getName()); model.addAttribute("age", user.getAge()); return "success"; } } } html <!DOCTYPE html> <html> <head> </head> <body> <ul> <th:block th:each="error : ${errors}"> <li th:text="${error.defaultMessage}"></li> </th:block> </ul> </body> </html> html <!DOCTYPE html> <html> <head> </head> <body> </body> </html>


上一篇:
下一篇:
切换中文