JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»


■    Arrays of all types


■    Enumerations—objects of type java.util.Enumeration


■    Iterators—objects of type java.util.Iterator.


We ’re going to put this functionality into a single tag so its users will be able to use one tag for all their iteration chores. They will be able to reference the object they want to iterate in the same way as in chapter 8, using Java reflection. In fact, we’ll reuse the reflection code we saw in chapter 8’s RefiectionTag to accomplish this. In doing so, our tag will be able to take any bean property value and iterate its objects. For example, we will be able to take a shopping cart with a method such as:


public Enumeration getProducts();


and iterate on the Enumeration value returned from it.

10.3.1    Design considerations for IterateTag


Given that we have the generic iteration infrastructure, and that we have a previously built basic reflection tag, implementing our tag should be a breeze (almost codeless, you might expect). But this is not quite the case because a Java class cannot inherit two superclasses (no multiple inheritance, if you recall). Also, our ReflectionTag did not implement BodyTag; instead, it implemented the Tag interface, so it cannot serve as a base class for an iteration-related tag. As a result, our iteration tag will have to reimplement the reflection code that we previously developed. There are ways to share the implementation code between the tags, but for simplicity’s sake, we will merely copy and paste the needed code.

10.3.2    Wrapping iterators


We will use the ReflectionTag code from chapter
8 to procure the referenced object from within the iteration tag, but we still need to decide what to do with it; meaning, how are we going to wrap it within an IterationSupport? We choose to create an IterationSupport implementation for each of the different iterator types (Iterator, Enumeration, and Array), then wrap the object within the matching IterationSupport implementation. An IterationSupport wrapper for the Iterator interface was covered in the previous section, so let’s now look at the individual wrappers for Array and Enumeration.

Скачать в pdf «JSP Tag Libraries»