I'm using JSTL to loop over a list of shop objects. It looks like the following:
<c:forEach items="${shops}" var="shop">
<div class="odd">
<li class="table-shop">${shop.name}</li>
开发者_开发问答 </div>
</c:forEach>
Now I want to be able to get the shop's position in the list. For example if it's the first shop, I'd like to print out 0
next to the name of the shop.
What's the best way I do this?
Use varStatus
, e.g.:
<c:forEach items="${shops}" var="shop" varStatus="loop">
<div class="odd">
<li class="table-shop">${loop.index} ${shop.name}</li>
</div>
</c:forEach>
<c:forEach items="${shops}" var="shop" varStatus="status">
<div class="odd">
${status.count}
<li class="table-shop">${shop.name}</li>
</div>
</c:forEach>
精彩评论