JSP Tag Libraries

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


We’ve now built a useful BodyTag and a reusable base class that will be helpful in upcoming examples. Later chapters will build on this section to provide high-level functionality such as conditioning (chapter 9) and iteration (chapter 10) but the fundamental principles we’ve learned here will not change.

6.8 Tag cooperation through nesting


A very powerful technique, though not as widespread as attribute use or even body manipulation, is tag nesting. Until now, none of our tags cooperated directly with any others. Admittedly, sometimes the execution of one tag affected another (such as in the case of the flush and redirect tags, and in our previous logger example), but this was not explicit cooperation. One tag acting alone cannot solve many real-world cases, which brings us to the need for some cooperation between different tags.


One obvious way that tags can cooperate is by using the JSP attributes (not to be confused with tag attributes). In this technique, tags use the JSP attributes as a shared memory space where they can exchange data. However, simple data exchange using the JSP attributes is not always sufficient. For example, what if we have a complex containment relation between two tags such that one tag has meaning only within the body of another? We surely cannot force such relations using the JSP attributes. When JSP attributes are used to coordinate two different tags, the JSP developer is typically required to name the different attributes (usually by providing an ID to the produced attribute) and to link the consumer of the attribute by again providing its name. Sometimes this is unnecessary extra work that can be resolved by another coordination technique. Indeed, JSP custom tags offer an implicit coordination technique by using parent-child relations among tags and the tag’s parent attribute. This is known as nesting.

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