Thymeleaf不显示Spring表单错误消息

Mar*_*ijk 27 spring thymeleaf

我正在将Spring jsp应用程序迁移到Thymeleaf但是在显示表单错误时遇到问题.

我正在使用SpringTemplateEngine和ThymeleafViewResolver并且模板的渲染工作.表单值也在表单输入字段中填充.

到目前为止唯一不起作用的是显示表单错误消息.

我的控制器看起来像:

@RequestMapping(method = RequestMethod.POST)
String save(@Valid CustomerForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) {
    if (bindingResult.hasErrors()) {
        model.addAttribute("form", form)
        return "app/customers/create"
    }
    ....
Run Code Online (Sandbox Code Playgroud)

我打印了bindingResult以验证它是否包含错误:

binding result = org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'customerForm' on field 'name': rejected value []; codes [customerForm.name.NotBlank,name.NotBlank,java.lang.String.NotBlank,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [customerForm.name,name]; arguments []; default message [name]]; default message [may not be empty]
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下方法显示错误:

<ul>
    <li th:each="e : ${#fields.detailedErrors()}" th:class="${e.global}? globalerr : fielderr">
        <span th:text="${e.global}? '*' : ${e.fieldName}">The field name</span> |
        <span th:text="${e.message}">The error message</span>
    </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

它不会显示任何错误.

我尝试了http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#validation-and-error-messages上记载的各种替代方案但没有成功.

我错过了什么吗?

编辑

注意我试图通过th:object在表单集中显示错误:

<form id="customer-form" action="#" th:action="@{/app/customers}" th:object="${form}" method="post" class="form-horizontal">
    <ul>
        <li th:each="e : ${#fields.detailedErrors()}" th:class="${e.global}? globalerr : fielderr">
            <span th:text="${e.global}? '*' : ${e.fieldName}">The field name</span> |
            <span th:text="${e.message}">The error message</span>
        </li>
    </ul>
</form>
Run Code Online (Sandbox Code Playgroud)

小智 56

我想你可能遇到了和我一样的问题 - 请看:

在那里回答Daniel Fernandez.基本上你的表单对象th:object="${form}" 是命名的,"form" 你的控制器正在寻找"customerForm"(类名)而不是 "form"(变量名)

可以重命名 @ModelAttribute("data")

从该链接复制使用:

public String post(@Valid FormData formData, BindingResult result, Model model){
    // th:object="${formData}"
}
Run Code Online (Sandbox Code Playgroud)

要么

public String post(@Valid @ModelAttribute("data") FormData data, BindingResult result, Model model){
    // th:object="${data}"
} 
Run Code Online (Sandbox Code Playgroud)

  • 是的,@ ModelAttribute("form")完成了这个伎俩.谢谢! (4认同)

Ble*_*zer 15

这就是我在表单中的表现:

为了显示所有错误,我把它放在表单的开头:

<div class="alert alert-danger" th:if="${#fields.hasErrors('*')}">
    <p th:each="err : ${#fields.errors('*')}" th:text="${err}"></p>    
</div>
Run Code Online (Sandbox Code Playgroud)

对于个别错误,我在字段之后添加它(当然,在hasErrors中更改字段以对应于测试的字段):

<p th:if="${#fields.hasErrors('vehicle.licensePlate')}" class="label label-danger" th:errors="*{vehicle.licensePlate}">Incorrect LP</p>
Run Code Online (Sandbox Code Playgroud)

如果这对您有用,请告诉我?