【问题标题】:Using Iteration variable in thymeleaf在百里香中使用迭代变量
【发布时间】:2014-01-07 15:09:57
【问题描述】:
您好,我是 thymeleaf 的新手,正在将旧项目从 jsp 转换为 thymeleaf。我正在尝试转换一段用 jsp 编写的代码:
<logic:iterate id="someForm" name="formName" property="nameList" indexId="i">
<%if (i%2==0)
{
className="even";
}
else
{
className="odd";
}
%>
//some code here
谁能帮我在百里香中转换这段代码??
【问题讨论】:
标签:
java
spring
jsp
jakarta-ee
thymeleaf
【解决方案1】:
万一你的 div 已经有一个类并且你想追加一个新类,使用 th:classappend:
<div class="col-md-12" th:each="propName,iterStat : ${propNames}" th:classappend="${iterStat.odd}? 'odd' : 'even'">
...
【解决方案2】:
您要查找的内容在 Thymeleaf documentation 中。
假设您需要遍历您的集合以显示 div 标签:
<div th:each="propName,iterStat : ${propNames}" th:class="${iterStat.odd}? 'odd' : 'even'">
...
</div