Creating striped lists/tables in JSTL
The JSTL iterator exposes a varStatus variable that holds the loop status. With this information at hand, we can constuct the following JSTL loop to create a list with alternate colors.
<ul>
<c:forEach var="row" items="${queryResults.rowsByIndex}" varStatus=’vs’>
<c:choose>
<c:when test= "${vs.index % 2 == 0}">
<li class=’even’>
</c:when>
<c:otherwise>
<li>
</c:otherwise>
</c:choose>
<c:out value="${row[0]}"/>
</li>
</c:forEach>
</ul>
The test condition checks whether the index property of varStatus variable is even or odd, and assigns a css class named ‘even’ to each even item.
You can easily substitute table rows for li elements in this example.
