background image

Custom Tag Examples

<< Evaluating Fragments Passed to Tag Files | Simple Attribute Example >>
<< Evaluating Fragments Passed to Tag Files | Simple Attribute Example >>

Custom Tag Examples

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,
Encapsulating Reusable Content Using Tag Files
Chapter 8 · Custom Tags in JSP Pages
243