场景:springboot+thymeleaf+jsr303
controller层:
//校验有错误返回
@PostMapping("/register")
public String register(@Valid RegisterForm registerForm, BindingResult result, Model model) {
if (result.hasErrors()){
return "register";
}
html:
<form th:action="@{/register}" method="post" id="formBtn" th:object="${registerForm}">
<input id="userName" name="username" type="text" th:field="*{username}">
<span th:if="${#fields.hasErrors('username')}" th:errors="*{username}"></span>
<input type="password" name="password" th:field="*{password}">
<span th:if="${#fields.hasErrors('password')}" th:text="*{password}"></span>
运算报错:Error during execution of processor 'org.thymeleaf.spring5.processor
解决:
在controller中添加对应校验实体
@ModelAttribute("registerForm")
public RegisterForm registerForm(){
return new RegisterForm();
}