Article

Introduction to the Java Standard Tag Library

Page: 1 2 3 4 Next

Conditional and Iterative Operations

Most of the time, JSP scriptlets are used to perform conditional (if..else) or iterative (for, while) tasks. The JSTL core tags provide this functionality without having to resort to scriptlets. The <c:if …> and <c:choose …> tags provide the basic conditional operations. For example, a simple if statement would read:

<c:if test="${homer.iq < 50}">  
Doh! My IQ is <c:out value=”${homer.iq}”/>                                            
</c:if>

To perform an if…else operation we can use the <c:choose> tag, like this:

<c:choose>  
<c:when test="${homer.iq < 50}">  
 Doh!  
</c:when>  
<c:when test="${homer.iq > 50}">  
 Wohoo!  
</c:when>  
     <c:otherwise>  
   An IQ of 50? Wohoo!    
</c:otherwise>  
</c:choose>

Looping can be performed using the <c:forEach …> tag:

<c:forEach var="braincell" items="${homer.braincells}">
<br /><c:out value="${braincell}"/>
</c:forEach> /#epc#/

This will display:
Eating  
Drinking  
Sleeping

The forEach tag provides us some optional attributes. For example, we can write:

<c:forEach items="${homer.braincells}" var="braincell" begin="1" end="2" varStatus="status">  
 <br />Braincell <c:out value="${status.count}"/>: <c:out value="${braincell}" />  
</c:forEach>

The above will display:
Braincell 1: Drinking    
Braincell 2: Sleeping

Here we’ve used the begin and end parameters to define where the loop will start and terminate. We also use the varStatus object. This object provides an interface into the state of the loop. Check the resources at the end for further information.

JSTL and URL Generation

The JSTL also provides tags to help us tidy up URL generation. Scriptlet code is often used to construct URLs that pass parameters to other pages, and can get messy very quickly. With the JSTL we can construct URLs in a much neater fashion. For example:

<c:url var="thisURL" value="homer.jsp">  
<c:param name="iq" value="${homer.iq}"/>  
<c:param name="checkAgainst" value="marge simpson"/>  
</c:url>  
<a href="<c:out value="${thisURL}"/>">Next</a>

The above would generate a URL that reads:

homer.jsp?iq=24&checkAgainst=marge+simpson

Notice how the JSTL has encoded the checkAgainst parameter for use in the URL, replacing the space with a +. No more worrying about invalid URLs! One extra bonus of using the url tag is to aid those surfing without session cookies enabled in their browser. The JSTL will check and automatically add a sessionId to the link if session cookies have been disabled.

Finally, a quick mention of the format tags. These are separate from the core tags, and have to be imported with the statement thus:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

The above appears at the top of the JSP page. The format tags provide powerful text formatting functions. Just as a brief experiment, try running the following in a JSP page:

<fmt:formatNumber value="34.5423432426236" maxFractionDigits="5" minFractionDigits="3"/>  
<fmt:formatNumber value="34.5423432426236" type="currency"/>  
 
<fmt:parseDate value="2/5/53" pattern="dd/MM/yy" var="homersBirthday"/>  
<fmt:formatDate value="${homersBirthday}" dateStyle="full"/>

The above example parses a date and then returns the date in a more verbose style. We can even add locale specifics to date formatting, for example:

<fmt:timeZone value="US/Eastern">  
<fmt:parseDate value="2/5/53" pattern="dd/MM/yy" var="homersBirthday"/>  
<fmt:formatDate value="${homersBirthday}" dateStyle="short"/>  
</fmt:timeZone>

Would output:

5/2/53

If you liked this article, share the love:
Print-Friendly Version Suggest an Article