田野是对象来自百里香还是春天

时间:2018-05-21 21:39:17

标签: spring thymeleaf spring-validator

我看到了使用Thymeleaf和Spring启动进行表单验证的以下内容。

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

现在我不明白:这个领域对象来自哪里?这是Spring准备的吗?我在这里迷路了。

2 个答案:

答案 0 :(得分:0)

您可以查看百里香的文档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:错误来自百日咳模板。 Spring Boot没有任何内容。因为这是您申请的前端。

让我们有输入字段,例如使用Thymeleaf,你提交的是字段,然后当你调用fields.hasErrors时,它会看到这个字段的任何错误。

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

答案 1 :(得分:0)

Fields.java是包org.thymeleaf.spring4.expression.Fields

中提供的课程

thymeleaf-spring4-xxx.jar

引入

#fields.hasErrors是调用hasErrors()类的方法Fields的语法。

public boolean hasErrors(final String field) {
    return FieldUtils.hasErrors(this.configuration, this.processingContext, field);
}