- 7.1 Tag Library Components
- 7.2 Example: Simple Prime Tag
- 7.3 Assigning Attributes to Tags
- 7.4 Example: Prime Tag with Variable Length
- 7.5 Including Tag Body in the Tag Output
- 7.6 Example: Heading Tag
- 7.7 Example: Debug Tag
- 7.8 Creating Tag Files
- 7.9 Example: Simple Prime Tag Using Tag Files
- 7.10 Example: Prime Tag with Variable Length Using Tag Files
- 7.11 Example: Heading Tag Using Tag Files
7.9 Example: Simple Prime Tag Using Tag Files
Let's rewrite the simple prime custom tag example using tag files. Listing 7.17 shows simplePrime2.tag. It consists of just one line invoking the static method nextPrime of the Primes class. The Primes.java file is shown in Listing 7.4. We place the simplePrime2.tag file into the WEB-INF/tags directory. Listing 7.18 shows simple-primes-2.jsp, which uses our JSP-based custom tag. Note that the taglib directive no longer has a uri attribute, but uses a tagdir attribute instead. This attribute tells the container which directory contains the tag files. Figure 7-6 shows the result of simple-primes-2.jsp.
Listing 7.17. simplePrime2.tag
<%= coreservlets.Primes.nextPrime (coreservlets.Primes.random(50)) %>
Listing 7.18. simple-primes-2.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Some 50-Digit Primes</TITLE> <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> </HEAD> <BODY> <H1>Some 50-Digit Primes</H1> <%@ taglib tagdir="/WEB-INF/tags" prefix="csajsp" %> <UL> <LI><csajsp:simplePrime2 /> <LI><csajsp:simplePrime2 /> <LI><csajsp:simplePrime2 /> <LI><csajsp:simplePrime2 /> </UL> </BODY></HTML>
Figure 7-6 Result of simple-primes-2.jsp.