对于Thymeleaf的每个运营商

And*_*niy 23 foreach spring for-loop spring-mvc thymeleaf

我找不到在Thymeleaf模板中构建简单for-each-loop的语法.我对just th:each=""属性不满意,因为它复制了它所在的标签.

我正在寻找的是:

<th:foreach th:each="...">
...block to be repeated...
</th>
Run Code Online (Sandbox Code Playgroud)

什么是模拟<c:forEach items="..." var="...">或<t:loop source="..." value="...">在Tapestry.有什么相似的吗?

eke*_*iga 47

th:block按照Thymeleaf指南中的说明使用

th:block是一个纯粹的属性容器,允许模板开发人员指定他们想要的任何属性.Thymeleaf将执行这些属性,然后简单地使块消失而无痕迹.

因此,在创建<tr>每个元素需要多个迭代表时,它可能很有用:

<table>
   <th:block th:each="user : ${users}">
      <tr>
         <td th:text="${user.login}">...</td>
         <td th:text="${user.name}">...</td>
      </tr>
      <tr>
         <td colspan="2" th:text="${user.address}">...</td>
      </tr>
   </th:block>
</table>
Run Code Online (Sandbox Code Playgroud)


Rai*_*baz 11

th:block解决方案绝对是最好的解决方案,但您也可以尝试使用th:remove="tag"以删除包含的标记:

<table>
   <tbody th:each="user : ${users}" th:remove="tag">
      <tr>
         <td th:text="${user.login}">...</td>
         <td th:text="${user.name}">...</td>
      </tr>
      <tr>
         <td colspan="2" th:text="${user.address}">...</td>
      </tr>
   </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

这种方法的好处是你也可以传递一个Thymeleaf表达式th:remove,以便只有条件地删除标签,例如,如果你只想要一些用户被包含在a中<tbody>,除了有其他有趣的用途.

这是文档th:remove.