collection of custom tag libraries for solving common problem such as;
- iteration, collection and conditional
- for processing xml document, formatting text, accessing database, and data manipulation
for manipulating scoped objects (out, set, remove) for performing conditional tests (if, choose, when, otherwise) for iterating over a collection or token (forEach, forTokens) parsing and formatting dates and numbers (parseNumber, formatNumber, parseDate, formatDate, etc)
forEach tag iterates over the phones property of an address scoped variable.
<c:forEach var="phone" items="${address.phones}">
${phone}"
</c:forEach>
The forEach tag has a varStatus variable of type javax.servlet.jsp.jstl.core.LoopTagStatus. The LoopTagStatus interface has the count property that returns the "count" of the current round of iteration.
Books in a styled table
<table>
<tr style="background: #ababff">
<td>ISBN</td>
<td>Title</td>
</tr>
<c:forEach items="${requestScope.books}" var="book" varStatus="status">
<c:if test="${status.count%2 == 0}">
<tr style="background: #eeeeff">
</c:if>
<c:if test="${status.count%2 != 0}">
<tr style="background: #dedeff">
</c:if>
<td>${book.isbn}</td>
<td>${book.title}</td>
</tr>
</c:forEach>
</table>
forEach is used to display the ISBNs in comma-delimited format. The use of status.last makes sure that a comma is not rendered after the last element.
<c:forEach items="${requestScope.books}" var="book"
varStatus="status">
${book.isbn}<c:if test="${!status.last}">,</c:if>
</c:forEach>
forEach to iterate over a map. The pseudocode for iterating over a map is as follows.
<c:forEach var="mapItem" items="map">
${mapItem.key} : ${mapItem.value}
</c:forEach>
forTokens tag to iterate over tokens that are seperated by the specified delimiters.
<c:forTokens var="item" items="om,Jerry,Donald" delims=",">
<c:out value="${item}"/><br/>
</c:forTokens>
In addition to custom actions, JSTL 1.1 and 1.2 define a set of standard functions you can use in EL expressions. These functions are grouped in the function tag library. To use the functions, you must use the following taglib directive on top of your JSP.
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions"
prefix="fn" %>
To invoke a function, you use an EL in this format.
${fn:functionName}
where functionName is the name of the function.
firstTag.tag To use this tag file as a tag extension, all you need to do is save it in the WEB-INF/tags directory of your application.
<%@ tag import="java.util.Date" import="java.text.DateFormat" %>
<%
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
Date now = new Date(System.currentTimeMillis());
out.println(dateFormat.format(now));
%>
firstTagTest.jsp
<%@ taglib prefix="easy" tagdir="/WEB-INF/tags" %>
Today is <easy:firstTag/>