求教用thymleaf实现一个jstl的效果

<c:set var="index" value="0" />
<c:forEach items="${cause}" var="c" varStatus="status">
    <c:set var="index" value="${index+1}" />
    <c:set var="id" value="title${index}" />

请问这样设置变量用thymleaf可以实现么

阅读 2.9k
1 个回答
<div th:each="law,lawStat:${cause}">
    <span th:id="'title'+${lawStat.index+1}" th:text="${law.law_name}"></span>

原来thymleaf的th:each自带下列属性,可以直接使用,之前以为不能这么用

index:当前迭代对象的index(从0开始计算)  
count: 当前迭代对象的index(从1开始计算)  
size:被迭代对象的大小  
current:当前迭代变量  
even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)  
first:布尔值,当前循环是否是第一个  
last:布尔值,当前循环是否是最后一个