The Java EE 5 Tutorial

Custom Tag Examples

This section introduces examples that demonstrate using custom tags.

Simple Attribute Example

The Duke’s Bookstore shipDate tag, defined in tut-install/javaeetutorial5/examples/web/bookstore3/web/WEB-INF/tags/shipDate.tag, is a custom tag that has a simple attribute. The tag generates the date of a book order according to the type of shipping requested.

<%@ taglib prefix="sc" tagdir="/WEB-INF/tags" %>
<h3><fmt:message key="ThankYou"/> ${param.cardname}.</h3><br>
<fmt:message key="With"/>
 <em><fmt:message key="${param.shipping}"/></em>,
  <fmt:message key="ShipDateLC"/>
<sc:shipDate shipping="${param.shipping}" />

The tag determines the number of days until shipment from the shipping attribute passed to it by the page tut-install/javaeetutorial5/examples/web/bookstore3/web/bookreceipt.jsp. From the number of days, the tag computes the ship date. It then formats the ship date.

<%@ attribute name="shipping" required="true" %>

<jsp:useBean id="now" class="java.util.Date" />
<jsp:useBean id="shipDate" class="java.util.Date" />
<c:choose>
  <c:when test="${shipping == ’QuickShip’}">
    <c:set var="days" value="2" />
  </c:when>
  <c:when test="${shipping == ’NormalShip’}">
    <c:set var="days" value="5" />
  </c:when>
  <c:when test="${shipping == ’SaverShip’}">
     <c:set var="days" value="7" />
  </c:when>
</c:choose>
<jsp:setProperty name="shipDate" property="time"
    value="${now.time + 86400000 * days}" />
<fmt:formatDate value="${shipDate}" type="date"
    dateStyle="full"/>.<br><br>

Simple and Fragment Attribute and Variable Example

The Duke’s Bookstore catalog tag, defined in tut-install/javaeetutorial5/examples/web/bookstore3/web/WEB-INF/tags/catalog.tag, is a custom tag with simple and fragment attributes and variables. The tag renders the catalog of a book database as an HTML table. The tag file declares that it sets variables named price and salePrice using variable directives. The fragment normalPrice uses the variable price, and the fragment onSale uses the variables price and salePrice. Before the tag invokes the fragment attributes using the jsp:invoke element, the web container passes values for the variables back to the calling page.

<%@ attribute name="bookDB" required="true"
    type="database.BookDB" %>
<%@ attribute name="color" required="true" %>
<%@ attribute name="normalPrice" fragment="true" %>
<%@ attribute name="onSale" fragment="true" %>

<%@ variable name-given="price" %>
 <%@ variable name-given="salePrice" %>

<center>
<table>
<c:forEach var="book" begin="0" items="${bookDB.books}">
    <tr>
    <c:set var="bookId" value="${book.bookId}" />
    <td bgcolor="${color}">
         <c:url var="url" value="/bookdetails" >
            <c:param name="bookId" value="${bookId}" />
        </c:url>
        <a href="${url}"><
            strong>${book.title}&nbsp;</strong></a></td>
     <td bgcolor="${color}" rowspan=2>
    <c:set var="salePrice" value="${book.price * .85}" />
    <c:set var="price" value="${book.price}" />
    <c:choose>
        <c:when test="${book.onSale}" >
            <jsp:invoke fragment="onSale" />
        </c:when>
        <c:otherwise>
            <jsp:invoke fragment="normalPrice"/>
        </c:otherwise>
    </c:choose>

    &nbsp;</td>

 ...
</table>
</center>

The page bookcatalog.jsp invokes the catalog tag that has the simple attributes bookDB, which contains catalog data, and color, which customizes the coloring of the table rows. The formatting of the book price is determined by two fragment attributes, normalPrice and onSale, that are conditionally invoked by the tag according to data retrieved from the book database.

<sc:catalog bookDB ="${bookDB}" color="#cccccc">
    <jsp:attribute name="normalPrice">
        <fmt:formatNumber value="${price}" type="currency"/>
    </jsp:attribute>
    <jsp:attribute name="onSale">
        <strike>
        <fmt:formatNumber value="${price}" type="currency"/>
        </strike><br/>
        <font color="red">
        <fmt:formatNumber value="${salePrice}" type="currency"/>
        </font>
    </jsp:attribute>
</sc:catalog>

The screen produced by tut-install/javaeetutorial5/examples/web/bookstore3/web/bookcatalog.jsp is shown in Figure 8–2. You can compare it to the version in Figure 5–2.

Figure 8–2 Book Catalog

Screen capture of Duke's Bookstore book catalog, with
titles, authors, prices, and "Add to Cart" links. Three discount prices are
highlighted.

Dynamic Attribute Example

The following code implements the tag discussed in Dynamic Attributes. An arbitrary number of attributes whose values are colors are stored in a Map named by the dynamic-attributes attribute of the tag directive. The JSTL forEach tag is used to iterate through the Map and the attribute keys and colored attribute values are printed in a bulleted list.

<%@ tag dynamic-attributes="colorMap"%>
<ul>
<c:forEach var="color" begin="0" items="${colorMap}">
    <li>${color.key} =
         <font color="${color.value}">${color.value}</font></li>
</c:forEach>
</ul>