Thymeleaf错误未显示Spring 4.x.

mfr*_*het 0 java spring spring-mvc thymeleaf

我实际上是在关注Spring验证教程,除了一件事,我可以让它正常工作.

实际上,如果我验证或不验证规则,重定向/模板返回工作会很好.

问题是我甚至没有在模板中看到错误消息.我不知道出了什么问题,我使用与教程相同的代码,但它没有显示任何内容(模板渲染得很好).

你可以帮帮我吗 ?

控制器:

@RequestMapping(value = "/print", method = RequestMethod.POST)
public String printPost(@Valid PrinterEntity printerentity, BindingResult bindingResult, Model model) {
    model.addAttribute("printed", printerentity.getName());
    model.addAttribute("printerentity", new PrinterEntity());

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

    return "printer/secretarea";
}
Run Code Online (Sandbox Code Playgroud)

模板视图(索引):

<form method="post" th:action="@{/print}" th:object="${printerentity}">

    <table>
        <tr>
            <td>
                <input type="text" th:field="*{name}"/>
            </td>
            <td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">
                Name Error
            </td>
        </tr>
        <tr>
            <td>
                <button type="submit">Valider</button>
            </td>
        </tr>
    </table>

</form>
Run Code Online (Sandbox Code Playgroud)

编辑:

表格页面:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">

    <meta charset="UTF-8"/>
    <title></title>
</head>
<body>
<h1 th:if="${printed != null}" th:text="${printed}"></h1>

<form method="post" th:action="@{/print}" th:object="${printerEntity}">

    <table>
        <tr>
            <td>
                <input type="text" th:field="*{name}"/>
            </td>
            <td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">
                Name Error
            </td>
        </tr>
        <tr>
            <td>
                <button type="submit">Valider</button>
            </td>
        </tr>
    </table>

</form>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

控制器:

@RequestMapping(value = "/print", method = RequestMethod.POST)
public String printPost(@Valid PrinterEntity printerEntity, BindingResult bindingResult, Model model) {
    model.addAttribute("printed", printerEntity.getName());
    model.addAttribute("printerEntity", new PrinterEntity());

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

    return "printer/nocontent";
}
Run Code Online (Sandbox Code Playgroud)

我可以在调试模式下看到错误的内容和断点,并探索que bindingResult对象.错误在,但似乎问题现在在模板渲染上,错误不会显示.

lgd*_*lgd 6

BindingResult使用camelCase中对象的类名将错误与对象相关联.

在您的情况下,字段错误printerEntity与否有关printerentity(您应该能够在调试模式下看到它).

如果printerEntity在模型中将对象重命名为,则会正确显示验证错误.

编辑

此外,您在模型中使用对象的新实例,而不是使用在索引上创建的实例以及在表单中使用的实例.

所以你的控制器看起来像这样:

@RequestMapping({"", "/"})
public String index(Model model) {
    model.addAttribute("printerEntity", new PrinterEntity());
    return "index";
}

@RequestMapping(value = "/print", method = RequestMethod.POST)
public String printPost(@Valid PrinterEntity printerEntity, BindingResult bindingResult, Model model) {
    model.addAttribute("printed", printerEntity.getName());
    model.addAttribute("printerEntity", printerEntity);

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

    return "printer/secretarea";
}
Run Code Online (Sandbox Code Playgroud)