Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
14.8 Using Nested Tags
343
There are two key new approaches for nested tags, however. First, nested
tags can use
findAncestorWithClass
to find the tag in which they are
nested. This method takes a reference to the current class (e.g.,
this
) and the
Class
object of the enclosing class (e.g.,
EnclosingTag.class
) as arguments.
If no enclosing class is found, the method in the nested class can throw a
Jsp
TagException
that reports the problem. Second, if one tag wants to store data
that a later tag will use, it can place that data in the instance of the enclosing
tag. The definition of the enclosing tag should provide methods for storing and
accessing this data. Listing 14.22 outlines this approach.
Listing 14.22 Template for Nested Tags
public class OuterTag extends TagSupport {
public void setSomeValue(SomeClass arg) { ... }
public SomeClass getSomeValue() { ... }
}
public class FirstInnerTag extends BodyTagSupport {
public int doStartTag() throws JspTagException {
OuterTag parent =
(OuterTag)findAncestorWithClass(this, OuterTag.class);
if (parent == null) {
throw new JspTagException("nesting error");
} else {
parent.setSomeValue(...);
}
return(EVAL_BODY_TAG);
}
...
}
public class SecondInnerTag extends BodyTagSupport {
public int doStartTag() throws JspTagException {
OuterTag parent =
(OuterTag)findAncestorWithClass(this, OuterTag.class);
if (parent == null) {
throw new JspTagException("nesting error");
} else {
SomeClass value = parent.getSomeValue();
doSomethingWith(value);
}
return(EVAL_BODY_TAG);
}
...
}
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved