Thymeleaf:如何在使用th时排除外部标签:每个?

Jul*_*uly 12 java template-engine thymeleaf

Thymeleaf 2.1.4官方文档演示了for each如下用法:

 <tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">
    <td th:text="${prod.name}">Onions</td>
    <td th:text="${prod.price}">2.41</td>
    ...
 </tr>
Run Code Online (Sandbox Code Playgroud)

它<tr>在每次迭代中生成一个,这在这种情况下是完美的.但是在我的情况下,我不需要外部标签(这里<tr>).


我的用例是以<bookmark>递归方式生成标记,没有其他标记包含,<bookmark>标记必须包含名称和href属性.

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<body>

<div th:fragment="locationBookmark(location)">
    <bookmark th:each="map : ${location.subMaps}">
        <bookmark th:name="${map.name}"
                  th:href="'#'+${map.id}" th:include=":: locationBookmark(${map})">
        </bookmark>
    </bookmark>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

包括方面:

<bookmark th:include="bookmark : locationBookmark(${rootLocation})"/>
Run Code Online (Sandbox Code Playgroud)

非常感谢.

Xip*_*ipo 31

即使它可以使用th:remove="tag"我建议你使用th:block

<th:block th:each="map : ${location.subMaps}">
   <bookmark th:name="${map.name}"
        th:href="'#'+${map.id}" th:include=":: locationBookmark(${map})">
    </bookmark>
</th:block>
Run Code Online (Sandbox Code Playgroud)