【发布时间】:2018-12-16 09:44:28
【问题描述】:
大家,
我的 Thymeleaf 有问题,我尝试在我的 for: each 中设置变量“迭代器”。
默认情况下“iterator=0;”但如果条件为真,我设置“iterator+1”,但即使条件为真,我的变量也始终为 0。
我的下一个块条件需要这个“迭代器”,因为如果迭代器 ==1,我需要在我的视图中显示其他内容
在这里你可以看到我的代码:
<div class="col-2" th:each="t:${horaire.terrains}">
<h2 th:text="${t.nomTerrain}"></h2>
<table border="1px" >
<tr th:each="h,index : ${heures.subList(0,15)}" >
<td>
<th:block th:each="resa:${reservations}"
th:if="${resa.dateReservation eq date && resa.heureDebut eq h}">
<a>reserve</a>
<p th:with="iterator=${iterator+1} "></p>
<!--<p th:text="${iterator}"></p>-->
</th:block>
<th:block th:if="${iterator eq 0 }">
<a th:text="${h}" th:href="@{addResa(heure=${h} ,
terrain=${t.id} ,date=${date},date2=${index.index+1},
listeHeures = ${heures} )}"></a>
</th:block>
</td>
</tr>
<!--<a th:text="${h}" th:href="@{addResa(heure=${h} , terrain=${t.id}
,date=${date},date2=${index.index+1}, listeHeures = ${heures} )}">
</a>-->
</table>
</div>
在这里,在我的控制器中,您可以看到变量迭代器:
int iterator=0;
model.addAttribute("iterator",iterator);
【问题讨论】:
-
这是不可能的。您不能在 thymeleaf 中分配和更改类似的变量。通常,它们是只读的。
标签: spring-boot thymeleaf