The forEach tag lets you loop down comma-delimited strings. But, what if the tokens are delimited by something other than a comma? Or, what if more than one character can separate the tokens? Enter the forTokens tag. In addition to the six attributes available in forEach (var, begin, end, step, items, status), forTokens has a delims attribute. This attribute specifies the delimiters, just as with the second argument to the StringTokenizer constructor.
Hmm, forTokens sounds an awfully lot like forEach. Were the JSTL developers able to leverage the forEach code when developing forTokens ? Yes! And you can too. JSTL provides a class called IteratorTagSupport that lets you create custom tags that extend the behavior of the forEach tag.
The following jr code treats parentheses as delimiters and creates a bulleted list of colors.
<UL> <jr:forTokens var="color" items="(red (orange) yellow)(green)((blue) violet)" delims="()"> <LI><%= pageContext.getAttribute("color") %> </jr:forTokens> </UL>
Here is the jx equivalent:
<UL> <jx:forTokens var="color" items="(red (orange) yellow)(green)((blue) violet)" delims="()"> <LI><jx:expr value="$color"/> </jx:forTokens> </UL>
Listing 12.25 shows a JSP page that uses the jr code; Figure 12-16 shows the result. Listing 12.26 shows a JSP page that uses the jx code; Figure 12-17 shows the result.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Token Loop: "jr" Version</TITLE> <LINK REL=STYLESHEET HREF="../styles.css" TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> Token Loop: "jr" Version </TABLE> <P> <%@ taglib uri="http://java.sun.com/jsptl/ea/jr" prefix="jr" %> <UL> <jr:forTokens var="color" items="(red (orange) yellow)(green)((blue) violet)" delims="()"> <LI><%= pageContext.getAttribute("color") %> </jr:forTokens> </UL> </BODY> </HTML> |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Token Loop: "jx" Version</TITLE> <LINK REL=STYLESHEET HREF="../styles.css" TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> Token Loop: "jx" Version </TABLE> <P> <%@ taglib uri="http://java.sun.com/jsptl/ea/jx" prefix="jx" %> <UL> <jx:forTokens var="color" items="(red (orange) yellow)(green)((blue) violet)" delims="()"> <LI><jx:expr value="$color"/> </jx:forTokens> </UL> </BODY> </HTML> |
One of the nice things about the forTokens tag is that it makes it easy to create nested loops. For example, the following jr code creates two bulleted lists: the first giving colors and the second giving numbers. The two sets of items are separated by parentheses. Within each set, individual items are separated by commas.
<jr:forTokens var="entry" items="(purple,cyan,black,green)(pi,e,7,6.02 x 10^23)" delims="()"> <UL> <jr:forTokens var="subentry" items='<%= (String)pageContext.getAttribute("entry") %>' delims=","> <LI><%= pageContext.getAttribute("subentry") %> </jr:forTokens> </UL><HR> </jr:forTokens>
Here is the jx equivalent:
<jx:forTokens var="entry" items="(purple,cyan,black,green)(pi,e,7,6.02 x 10^23)" delims="()"> <UL> <jx:forTokens var="subentry" items="$entry" delims=","> <LI><jx:expr value="$subentry"/> </jx:forTokens> </UL><HR> </jx:forTokens>
Listing 12.27 shows a JSP page that uses the jr code; Figure 12-18 shows the result. Listing 12.28 shows a JSP page that uses the jx code; Figure 12-19 shows the result.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Nested Token Loop: "jr" Version</TITLE> <LINK REL=STYLESHEET HREF="../styles.css" TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> Nested Token Loop: "jr" Version </TABLE> <H3>Favorite Colors and Numbers:</H3> <HR> <%@ taglib uri="http://java.sun.com/jsptl/ea/jr" prefix="jr" %> <jr:forTokens var="entry" items="(purple,cyan,black,green)(pi,e,7,6.02 x 10^23)" delims="()"> <UL> <jr:forTokens var="subentry" items='<%= (String)pageContext.getAttribute("entry") %>' delims=","> <LI><%= pageContext.getAttribute("subentry") %> </jr:forTokens> </UL><HR> </jr:forTokens> </BODY> </HTML> |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Nested Token Loop: "jx" Version</TITLE> <LINK REL=STYLESHEET HREF="../styles.css" TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> Nested Token Loop: "jx" Version </TABLE> <H3>Favorite Colors and Numbers:</H3> <HR> <%@ taglib uri="http://java.sun.com/jsptl/ea/jx" prefix="jx" %> <jx:forTokens var="entry" items="(purple,cyan,black,green)(pi,e,7,6.02 x 10^23)" delims="()"> <UL> <jx:forTokens var="subentry" items="$entry" delims=","> <LI><jx:expr value="$subentry"/> </jx:forTokens> </UL><HR> </jx:forTokens> </BODY> </HTML> |
3.135.202.203