10.2 Generalized iterating tags
In perusing the implementation of SimpleForeachTag it appears that most of the
work done by the tag is not unique to it. In fact, other than the creation of the Iterator
object in parseElements() all the other code was generic. True, some
tags will not want to expose an iterator, and others may want to expose more than a
single iterator as a scripting variable (for some other tag-specific purpose), but these
tags are not representative of the majority. In most cases, tags will differ only in the
objects they iterate (some will iterate over an Enumeration, others on Array, etc.)
but the general structure will stay the same; a single iterator scripting variable will be
exposed and updated for each element.
Based on this general iterating structure, we'll build:
- A generic iteration interface that lets the tag developer specify how to iterate
over some set of objects. .
- A basic iterator tag that takes a generic iteration object (Enumeration, Array, etc.) and iterates on it.
Creating these two, generic components will then streamline the creation of various
iteration tags. These specialized iteration tags will be custom-built, based on the
type of Java object to be contained in the iterator, and the iterator type in which
these objects are to be contained. For example, our SimpleForeachTag had an iterator
type of java.util.Iterator, and contained in that iterator was a list of Strings.
We are now going to build these two components (the class and interface)
and modify SimpleForeachTag to use this new, more generic infrastructure.
10.2.1 A generic iteration interface
Before looking into the new ForeachTag, let's study the generic iteration infra-structure
on which it is constructed, starting with the generic iteration interface as
seen in listing 10.4.
Listing 10. 4 Source code for the generic iteration interface
package book.iteration;
import javax.servlet.jsp.JspException;
public interface IterationSupport {
public boolean hasNext()
throws JspException;
public Object getNext()
throws JspException;
}
Why do we need another iteration/ enumeration interface, as Java already offers
plenty. You may also wonder, why a JspException is thrown from the methods hasNext()
and getNext(). Shouldn't a generic interface remove JSP related ties?
We do this because we want to provide better JSP integration. Let's explore our
motivation for this integration.
NOTE We could consider the option of defining a new exception type (such as
IterationException) that the iteration support methods could throw;
but why should we? This code is written for the JSP tags, and we are not going to reuse it. In 99 percent of all cases, you are going to throw a
JspException as a result of the error. Based on this argument, we've rejected
the new exception type idea, and continue to use JspException as our error-reporting vehicle.
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.
|