首页 > 解决方案 > 是百里香叶还是春天的字段对象

问题描述

我看到如下使用 Thymeleaf 和 Spring boot 进行表单验证的内容。

<p th:if="${#fields.hasErrors('datePlanted')}" th:errors="*{datePlanted}">Incorrect date</p>

现在我不明白:这个字段对象来自哪里?这是Spring准备的东西吗?我在这里的某个地方迷路了。

标签: springthymeleafspring-validator

解决方案


您可以查看 thymeleaf 文档https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#field-errors

<p th:if="${#fields.hasErrors('datePlanted')}" th:errors="*{datePlanted}">Incorrect date</p>

这里的所有标签如 th:if, th: 错误都来自 thymeleaf 模板。这里没有任何东西来自 Spring Boot。因为这是您的应用程序的前端。

让我们输入字段,例如使用 Thymeleaf 并且您正在提交这是字段,然后当您调用 fields.hasErrors 时,它会查看此字段的任何错误。

<input type="hidden" class="form-control" placeholder="Name"
                    th:field="*{id}" th:value="*{id}" />

推荐阅读