Cooperating Tags

Cooperating tags are those that share information in some way. JSP information can be shared using the following mechanisms:

  • Shared variables

  • Hierarchical (or nested) tags

Using Shared Variables

One way wayway of writing cooperating tags is to use EL or scripting variables to pass information between the tags. As previously discussed a tag can create a variable that can be retrieved by an another tag on the current page. Depending on the scope of the variable, it can be passed on to other pages in the same request or to pages in subsequent requests. Variables defined by custom tags have the same scoping rules as other variables defined on the page. Using variables are a very flexible means of passing information between tags.

Hierarchical Tag Structures

An alternative means of passing information between tags is to use a parent/child (or hierarchical) relationship. The parent (outer tag) contains information that is accessed by a child (inner) tag. The parent tag is often an iterative tag, and the child is used to retrieve information for each iteration.

Two static methods are provided in the javax.servlet.jsp.tagext.TagSupport class for finding a parent tag from within the child:

  • TagSupport.findAncestorWithClass(from, class) This method searches through the tag hierarchy until it finds a tag with the same class as the second parameter. The first parameter defines the start point of the search and is typically the this object.

  • TagSupport.getParent() This method finds the immediately enclosing parent tag.

The advantage of this approach over sharing variables is that the information can only be used in the correct context. The scope of the information can be constrained to the Web page between the start and end tags of the parent tag.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.106.33