Iterating with tags
In this chapter
- Iterating with tags 101
- Universal iteration with tags (iterate anything!)
- Tag-only presentation of a shopping cart
- The JSP1.2 IterationTag Iterating With Tags
At the end of chapter 8, we used our newly created JavaBean tags to export an Enumeration
which was then iterated over with a scriptlet. Let's take another look at
this JSP fragment.
<table>
<tr>
<th> Header </ th> <th> Value </th>
</tr>
<bean: export id="e"
type=" enum"
object="<%= request %>"
property="headerNames" />
<% while(e.hasMoreElements()) {
String name = (String)e.nextElement();
%>
<tr>
<td> <%= name %> </td>
<td>
<bean: show object="<%= request %>"
property=" header"
index="<%= name %>"/>
</td>
</tr>
<%
}
%>
</table>
As you can see (note the highlighted code), although our JavaBean tags greatly
reduce the need for scriptlets, we are still unable to avoid them when working with
indexed JavaBean properties that have more than one value. In cases of multivalued
properties (Enumerations, arrays, etc.) we typically want to loop through (iterate)
and perform a function with each value in the property. Without a tag to handle this
iteration, we're left using a scriptlet like the one here. This is unfortunate since we
want to be able to provide our JSP authors with the ability to perform common
functions on JavaBeans without prior knowledge of Java. Ideally, we'd like to offer
them a very user-friendly JSP custom tag that would work for iteration.
Iteration, especially enumerating some value, can be very declarative, and, as
we've seen, declarative tasks are easily performed with tags. For example, by using
iteration tags we can modify the previous JSP fragment:
<table>
<tr>
<th> Header </th> <th> Value </th>
</tr>
<iter:foreach id="name"
type="String"
object="<%= request %>"
property="headerNames" />
<tr>
<td> <%= name %> </td>
<td>
<bean: show object="<%= request %>"
property=" header"
index="<%= name %>"/>
</td>
</tr>
<iter:foreach>
</table>
This is obviously quite an improvement.
Why should we bother creating special iteration tags when a two-line scriptlet
hardly seems demanding for a Java developer? Again, we can't forget that the goal of
building custom tag libraries is to make it possible for non-Java developers (presentation/ HTML
developers) to build complex sites. Though iteration using scriptlets
may not be complex for the Java programmer, it does require the JSP developer to:
- Know how to iterate on different Java types— Enumerations, Iterators,
arrays, and so forth. To further complicate the situation, iteration methods
usually return an Object that the JSP developer will have to cast. .
- Position the curly brackets in the correct location. If the JSP developer forgets
a curly bracket, the JSP compilation will fail, usually with a relatively obscure
error message. .
- Maintain and debug yet another portion of Java code.
As a result, iteration tags are necessary to enhance the effectiveness of our JavaBean
tags and to keep our JSPs scriptlet-free.
This chapter explores iteration with tags and shows how to build JSP custom
tags that perform iteration for us. We'll start with a brief introduction to iterating
with custom JSP tags and discuss their design principles; later, we will develop iteration
tags to handle cases in which we wish to iterate over Java's common object
containers.
NOTE In this chapter, you will see the word iterator used in two distinct ways.
When we use the generic term, we mean any multivalued object (be it an Array, an implementation of the java.util.Enumeration interface or
an implementation of the java.util.Iterator interface). When we mention Iterator we are speaking strictly about the Java interface.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|