I would like to write a custom JSP tag whose output includes other JSP tags which should themselves also be dynamically evaluated. But apparently everything that my TagSupport
subclass writes to pageContext.getOut()
just goes straight to the client without any further evaluation.
I have a feeling this should be very simple, since it seems like one of the very first things one would want to use custom tags for: encapsulating and reusing other custom tags, avoiding code duplication.
How do I make the following code do what it obviously wants to do?:
public class MyTag extends TagSupport {
public int doStartTag() throws JspException {
try {
pageContext.getOut().println(
"The output from this tag includes other tags " +
"like <mypackage:myOtherTag>this one</mypackage:myOtherTag> " +
"which should themselves be evaluated and rendered."
)
} catch (IOException e) {
throw new JspException(e);
}
return SKIP_BODY;
}
}
Edit: Some background on my particular use case, if it helps. I have a custom tag <user>
which dynamically renders a user name in a way that is useful for my application (mouse-hover for first name, last name, phone number, etc.). I'm now writing another tag <comment>
for displaying user comments, and I would like to use my existing <user>
tag for rendering user names in the output of the <comment>
tag.
You could split your classes into a tag class and a
tagRenderer
class.In your situation there would be two new classes called
CommentTagRenderer
andUserTagRenderer
.Here is an example of the new
CommentTag
And here is an example of the
CommentTagRenderer
The easiest way is to write your custome tag as a JSP tag file, rather than in Java. This way, the new tag can use other custom tags easily. Create a file myTag.tag in /WEB-INF/tags, and use the following code:
More information about tag files here: http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html