Thursday, 14 August 2008

JSTL Core Tag Library: case sensitivity and finding last element in forEach

Prelude:
Have been a really good girl recently after being moved into a new team at work! : P Working with newer technologies and finally getting to apply all the knowledge and details I studied from my Java SCWCD exam!!! Yip it feels pretty good and pretty practical too! : ) I already have noted down quite a few things that I have noted down to blog in the future. Meanwhile let me go back to the wonder world of the JSTL Core Tag Library first........


Case Sensitivity:
Actually this is related to using tags on a JSP page rather than just the core tag library. The case of the tags matter on a JSP page. For example:
If you use write on your page, the tag will not be recognised!
This is because the actual tag is defined as . So case sensitivity matters when using tag library.

forEach and Finding the Last Element in a Collection:
The tag has an attribute varStatus which allow you to obtain information such as the current index the loop is currently at. If you use varStatus.last, it will return true if the current index is the last element in the collection. For example:
<c:foreach var="current" items="${bean.collection}" varstatus="status">
${current.value}<c:if test="${not status.last}">,</c:if>
</c:foreach>
The above code will add comma to the end of every item in the collection EXCEPT for the last one. So the varStatus is actually very useful indeed. : P very handy!

1 comment:

  1. > Have been a really good girl recently......

    are you sure......?

    ReplyDelete