繁体   English   中英

Thymeleaf不显示Spring表单错误消息

[英]Thymeleaf not displaying Spring form error messages

我正在将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>

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

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

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

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

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

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

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

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

注意:对象名称不是类名

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

class User{
    @NotBlank
    String username;

    @Length(min=6)
    String password;
}

假设在控制器中,我们在验证“ user ”下命名对象,请参阅@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";
}

在上面的控制器片段中,对象名称是“ user ”而不是“User”

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM