background image

Cooperating Tags

<< TagExtraInfo Class | Private Objects >>
<< TagExtraInfo Class | Private Objects >>

Cooperating Tags

The web container passes a parameter of type javax.servlet.jsp.tagext.TagData to the
getVariableInfo
method, which contains attribute-value tuples for each of the tag's attributes.
These attributes can be used to provide the VariableInfo object with an EL variable's name and
class.
The following example demonstrates how to provide information about the variable created by
the iterator tag in a tag extra info class. Because the name (var) and class (type) of the
variable are passed in as tag attributes, they can be retrieved using the
data.getAttributeString
method and can be used to fill in the VariableInfo constructor. To
allow the variable var to be used only within the tag body, you set the scope of the object to
NESTED
.
package iterator;
public class IteratorTEI extends TagExtraInfo {
public VariableInfo[] getVariableInfo(TagData data) {
String type = data.getAttributeString(
"type");
if (type == null)
type =
"java.lang.Object";
return new VariableInfo[] {
new VariableInfo(data.getAttributeString(
"var"),
type,
true,
VariableInfo.NESTED)
};
}
}
The fully qualified name of the tag extra info class defined for an EL variable must be declared in
the TLD in the tei-class subelement of the tag element. Thus, the tei-class element for
IteratorTei
would be as follows:
<tei-class>
iterator.IteratorTEI
</tei-class>
Cooperating Tags
Tags cooperate by sharing objects. JSP technology supports two styles of object sharing.
The first style requires that a shared object be named and stored in the page context (one of the
implicit objects accessible to JSP pages as well as tag handlers). To access objects created and
named by another tag, a tag handler uses the pageContext.getAttribute(name,scope)
method.
In the second style of object sharing, an object created by the enclosing tag handler of a group of
nested tags is available to all inner tag handlers. This form of object sharing has the advantage
that it uses a private namespace for the objects, thus reducing the potential for naming conflicts.
Programming Simple Tag Handlers
Chapter 8 · Custom Tags in JSP Pages
263