Iterating with tags 101 to export the values
CHAPTER 10 Iterating with tags elements=”1,2,3,4″> The selected item is < %= item %>
Now when we execute our JSP, SimpleForeachTag will repeat its body four times (one for each string in elements ); first with 1 as the value of the item (our iterator), and lastly with 4 as its value. 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 infrastructure on which it is constructed, starting with the generic iteration interface as seen in listing 10.4.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services