spring - Thymeleaf:如何在表达式中使用片段参数

标签 spring spring-mvc thymeleaf

有没有办法在表达式中使用片段参数?

我想创建一个片段来显示具有相应绑定(bind)错误的字段,例如喜欢:

<div th:fragment="alert (field, fieldLabel)">
    <label><span th:text="${fieldLabel}">Label:</span><input type="text" th:errorclass="field_error" th:field="*{field}"/></label>
    <div th:if="${#fields.hasErrors(field)}"><span th:errors="*{field}">Some error</span></div>
</div>

获取片段:

<div th:replace=":: alert (field='firstName', fieldLabel='Firstname')">Field</div>

如何在 th:field 和 th:errors 属性的表达式中使用 field 参数? *{field} 至少不起作用。

最佳答案

在 Thymeleaf 2.1 中,我一直在使用以下内容:

声明字段:

<input type="password" th:field="*{password}" />

显示可能与密码相关的错误:

<div th:replace="util/form :: field-errors('password')"></div>

这会打印出与给定字段相关的所有错误:

<div class="error-container help-block"
        th:fragment="field-errors(field)"
        th:if="${#fields.hasErrors('__${field}__')}">
    <ul>
        <li th:each="error : ${#fields.errors('__${field}__')}"
            th:text="${error}" />
    </ul>
</div>

关于spring - Thymeleaf:如何在表达式中使用片段参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22098911/

相关文章:

java - 没有主类的spring boot

java - 缺少 Pivotal Tomcat Server Developer Edition v3.0-config

java - 未加载 Spring Boot 模板文件

spring-mvc - thymeleaf 模板中的 JSON 序列化对象

java - 如果缺少 @PreAuthorize 注释,则 Spring Security : Deny access to controller methods,

spring security,如何使用户的所有 session 过期

java - 将常见的 taglibs、js、css 文件移动到一个文件中

java - Thymeleaf 下拉选择

spring - 如何在 ubuntu 中使用命令行在后台运行基于 gradle 的 spring boot 应用程序?

java - 创建 ServletContext 资源中定义的名称为 'jsonView' 的 bean 时出错