我正在用百里香做一个SpringBoot应用程序,但是我无法使验证器消息起作用。我收到有关validation.propeties的消息,但总是收到错误消息:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/app/person/addPerson.html]")] with root cause
org.springframework.context.NoSuchMessageException: No message found under code 'NotEmpty.personForm.name' for locale 'en_US'.
我尝试将应用程序设置为在配置文件中使用固定语言,并尝试创建validate_en_US.propeties。
validator.properties:
NotEmpty=This field is required.
PersonValidador.java
@Override
public void validate(Object o, Errors errors) {
Person user = (Person) o;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "NotEmpty");
}
PersonController.java
@PostMapping("/app/addperson")
public String addPerson(@ModelAttribute("personForm") Person person, BindingResult bindingResult) {
System.out.println(person.getName());
personValidator.validate(person, bindingResult);
if (bindingResult.hasErrors()) {
return "app/person/addPerson";
}
return "app/person/listPerson";
}
addPerson.html
<form role="form" th:action="@{/app/addperson}"
th:object="${personForm}" method="POST">
<div class="box-body">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="name">Name</label>
<input th:field="*{name}" th class="form-control" id="name" placeholder="" type="text">
<ul th:if="${#fields.hasErrors('name')}">
<li th:each="err : ${#fields.errors('name')}" th:text="${err}">Input is incorrect</li>
</ul>
</div>
</div>
</div>
</div>
<div class="box-footer">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</form>
错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/app/person/addPerson.html]")] with root cause
org.springframework.context.NoSuchMessageException: No message found under code 'NotEmpty.personForm.name' for locale 'en_US'.
我不知道我在做什么错。有人可以帮我吗?