不是直接的,但您可以使用varStatus
将 的实例LoopTagStatus
放在 的范围内<c:forEach>
。它提供了几个 getter 来计算循环索引以及它是循环的第一次还是最后一次迭代。
我只是不确定你的<c:if>
意思是什么,但我认为你实际上有两个大小相同的列表,其中包含评论名称和评论率,并且你只需要显示与评论相同索引的比率。
<c:forEach items="${commentNames}" var="comment" varStatus="commentLoop">
${comment}
<c:forEach items="${rates}" var="rate" varStatus="rateLoop">
<c:if test="${commentLoop.index == rateLoop.index}">
${rate}
</c:if>
</c:forEach>
</c:forEach>
然而,这很笨拙。您可以直接按索引更好地获取费率。
<c:forEach items="${commentNames}" var="comment" varStatus="commentLoop">
${comment}
${rates[commentLoop.index]}
</c:forEach>
更好的是创建一个Comment
具有name
andrate
属性的对象。
public class Comment {
private String name;
private Integer rate;
// Add/autogenerate getters/setters.
}
这样您就可以按如下方式使用它:
<c:forEach items="${comments}" var="comment">
${comment.name}
${comment.rate}
</c:forEach>
也可以看看: