JSP JSTL Using varStatus to Add CSS Class On Last Element

Often when iterating a collection to display it on table specific css class need to be added to the last row for styling purpose, eg:


Jim
34


Bob
30


Susan
28

This can be achieved on JSP JSTL core tag by using varStatus attribute:


  
    ${user.name}
    ${user.age}
  

On varStatus you define what is the variable name which will hold the loop iteration status. This variable then contain information whether or not we’re on the last iteration, and place specific content accordingly. See the javadoc reference for LoopTagStatus for more info.

The question mark and colon syntax ${status.last ? 'class="last"' : ''} is similar to standard Java syntax. It will print the content class="last" only if status.last is true (ie: we’re on the last iteration of loop)

Leave a Reply