Thymeleaf不显示Spring表单错误消息 [英] Thymeleaf not displaying Spring form error messages

查看:193
本文介绍了Thymeleaf不显示Spring表单错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Spring jsp应用程序迁移到Thymeleaf,但无法显示表单错误.

I'm migrating a Spring jsp application to Thymeleaf but having problems displaying form errors.

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

I'm using the SpringTemplateEngine and ThymeleafViewResolver and rendering of templates works. Also form values are populated in form input fields.

到目前为止,唯一无法正常工作的是显示表单错误消息.

The only thing so far not working is displaying form error messages.

我的控制器如下:

@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"
    }
    ....

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

I printed the bindingResult to verify it contains an error:

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]

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

When I try to display error using:

<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>

它不显示任何错误.

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

I tried various alternatives as documented on http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#validation-and-error-messages but without success.

我想念什么吗?

编辑

请注意,我正在尝试通过th:object:

Note I'm trying to display the error within a form set via 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>

推荐答案

我认为您可能遇到的问题与我一样-请参阅:

I think you may be having the same issue as I did - please see :

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

There it is answered by Daniel Fernandez. Basically your form object th:object="${form}" is named "form" but your controller is looking for "customerForm" (class name) not "form" (the variable name)

可以用@ModelAttribute("data")

从该链接使用中复制:

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

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

这篇关于Thymeleaf不显示Spring表单错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆