Thymeleaf:如何在表达式中使用fragment参数

Jam*_*mes 3 spring spring-mvc thymeleaf

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

我想创建一个片段来显示带有相应绑定错误的字段,例如:

<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>
Run Code Online (Sandbox Code Playgroud)

获取片段:

<div th:replace=":: alert (field='firstName', fieldLabel='Firstname')">Field</div>
Run Code Online (Sandbox Code Playgroud)

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

per*_*rak 9

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

声明字段:

<input type="password" th:field="*{password}" />
Run Code Online (Sandbox Code Playgroud)

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

<div th:replace="util/form :: field-errors('password')"></div>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)