Thymeleaf不显示Spring表单错误消息

时间:2014-04-08 21:10:32

标签: 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"
    }
    ....

我打印了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]

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

<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中记载的各种替代方案,但没有成功。

我错过了什么吗?

修改

注意我试图通过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>

3 个答案:

答案 0 :(得分:50)

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

Daniel Fernandez回答。基本上,您的表单对象th:object="${form}"名为"form" 您的控制器正在寻找"customerForm"(班级名称) "form"(变量名称)

可以使用@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}"
} 

答案 1 :(得分:13)

这就是我在表格中的表现方式:

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

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

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

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

请告诉我这是否适合您?

答案 2 :(得分:0)

添加到@Blejzer回答: 在messages.properties文件中命名错误消息必须遵循以下命名约定,因此将返回消息字符串而不是消息密钥:

(约束名称)。(对象名称)。(属性名称)

注意:对象名称不是类名

例如,如果您有以下用户类:

class User{
    @NotBlank
    String username;

    @Length(min=6)
    String password;
}

假设在控制器中,我们在验证“用户”下命名了对象,请参阅@ModelAttribute(“user”):

@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String signup(@Valid @ModelAttribute("user") User user, BindingResult bindingResult, Model model) {

    if (bindingResult.hasErrors()) {
        return "signup";
    }

    //do some processing

    return "redirect:/index";
}

在上面的控制器代码段中,对象名称为“用户”而不是“用户”

现在,要显示自定义消息,您必须将消息命名如下:

NotBlank.user.username=User Name is blank
Length.user.password=Password length must be at least 6 letters
相关问题