Skip to content

Alternating Row Color in JSP/JSTL

Here’s the simplest way I’ve found for alternating the background color of rows in JSP/JSTL:

<style type="text/css">
.row0 { background-color:#fff; }
.row1 { background-color:#ececec; }

<c:forEach items="${myItems}" var="myItem" varStatus="lineInfo">
<tr class="row<c:out value="${lineInfo.index % 2}"/>">

Posted in Java.

3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Jeff Perreault says

    Thanks for the post, just one thing.

    In order to get a number from lineInfo, I think you meant this:

  2. Jeff Perreault says

    <c:out value="${lineInfo.index % 2}"/>

  3. jtanium says

    You’re right, good catch! Thanks!

Some HTML is OK

or, reply to this post via trackback.