【问题标题】:Is fields object from thymeleaf or spring是百里香叶还是春天的字段对象
【发布时间】:2018-05-21 21:39:17
【问题描述】:

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

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

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

【问题讨论】:

    标签: spring thymeleaf spring-validator


    【解决方案1】:

    Fields.java 是包org.thymeleaf.spring4.expression.Fields 中的一个类

    thymeleaf-spring4-xxx.jar拉进来

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

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

    【讨论】:

    • 这个类是从哪里来的。你能给我更多的信息吗?谢谢
    • 是被thymeleaf-spring4-xxx.jar拉进来的。打开你的依赖 jar 并查看它
    【解决方案2】:

    您可以查看 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}" />
    

    【讨论】:

    • 我从文档中复制了该行。不是我正在寻找的答案
    • 好的。这个解释对你来说清楚吗?如您所见,#fields.hasErrors(...) 函数接收字段表达式作为参数 (datePlanted),并返回一个布尔值,告知该字段是否存在任何验证错误。来自文档:thymeleaf.org/doc/tutorials/2.1/…
    • 我的问题是这个字段对象来自哪里以及谁在设置它的属性。
    • 对不起。我更新了我的答案。你能再看一遍吗?
    • 我的问题是这个字段对象来自哪里以及谁在设置它的属性。
    猜你喜欢
    • 2015-12-09
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2018-08-01
    相关资源
    最近更新 更多