Using “varStatus” in JSTL

Recently, there was a requirement to display a list of entries(key,value) of a Map separated by comma in a JSP. An effective way of doing it is by using JSTL’s “c:forEach” tag and its attribute “varStatus” to iterate through the key-value pairs. The code is as follows:


(<c:foreach var="entry" items="${criticalityMap}" varstatus="status">
  <bean:message key="arhat.asset.criticality.${entry.key}"></bean:message> : ${entry.value}
  ${not status.last ? ',' : ''}
</c:foreach>)

Output:
( Low : 1 , Medium : 1 , High : 2 , Most Critical : 1 )

Courtesy:

Andrew Beacock’s blog

P.S. : There was a problem while pasting the above piece of code into the blog, which could be resolved again by an elegant solution by Greg Houston. Even this pointer, i had picked up from Andrew Beacock.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s