JSTL

JSTL c and fmt tag snippets, currency formatting with the fmt tag, and how to test for nulls

Published: Saturday, 7 January 2006

JSTL is a presentation layer taglib used in JSP.

URL rewrite for absolute links

Instead of maintaining relative links (it is confusing if your JSP fragments are in another directory to the including JSP), you can turn the links into absolute URLs with c:url. That way you can deploy the webapp into any context and the link will resolve correctly.

It can look a bit messy with nested tags.

<img src="<c:url value="/images/calendar-icon.gif"/>"/>

c:fmt currency formatting

To format a currency you can set type attribute to “currency”. The pattern is prefixed with a ¤, or \u00A4 denoting the currency symbol. The # is a digit that is shown if present, the 0 are digits padded with 0. This pattern is taken from the standard DecimalFormat class.

<fmt:formatNumber var="recentDividendRate" currencyCode="USD" value="${dividendRate}" type="currency" pattern="¤##0.00####"/>

null testing variables

Use ’not empty’ e.g.

<c:if test="${not empty recentDividend}">
  .. dividend present
</c:if>