how do u invoke Tag b's Tag Handler from within Tag a's tag Handler?

how do u invoke Tag b's Tag Handler from within Tag a's tag Handler?

Post by shrud » Thu, 26 Jan 2006 15:03:23

i want to have a custom tag to render tabs onto the screen. this tag
will have custom tags nested inside it which are the tabs that have to
be rendered and the tab content for each tab.

<myTab: Tab title="Tab1">
<jsp:include "Tab1_Content.jsp/>
</myTab: Tab>

<myTab: Tab title="Tab2">
<jsp:include "Tab2_Content.jsp/>
</myTab: Tab>

<myTab: Tab title="Tab3">
<jsp:include "Tab3_Content.jsp/>
</myTab: Tab>
</myTab: Tabs>

how can a tag Handler invoke the Tag handler of another tag?
Thanks in advance.

how do u invoke Tag b's Tag Handler from within Tag a's tag Handler?

Post by John C. Bo » Sat, 28 Jan 2006 12:00:42

Your question is a bit unclear. Objects cannot be invoked, though their
methods can be.

My best guess at what you mean is that you want one of your tag handlers
to invoke the life cycle methods of other tag handlers. Let me be very
clear: that is absolutely FORBIDDEN. Tag handlers' life cycle methods
are reserved for use by the JSP container. (And it wouldn't do what you
probably want, anyway.) If that was indeed what you were asking then do
not despair; you don't need to do it in the first place. Read on.

A tag handler can easily obtain a reference to any other tag handler
that logically contains it by use of TagSupport.findAncestorWithClass().
Via that reference, it can invoke any of the containing handler's
methods (but see above, and strong prohibitions also apply to attribute

You should also pay attention to the BodyTag interface. Chances are
good that the tag handlers you are working on will want to buffer their
bodies. I might design the system along these lines:

(1) [Architectural] The handler for myTab:Tabs provides a method
addTab([SomeAppropriateType]) that is intended for other tag handlers to use
(2) [Behavioral] The handler for myTab:Tabs buffers its body and
discards the result
(3) [Behavioral] The handler for myTab:Tab buffers its body, then in
doEndTag() it looks up the containing myTab:Tabs and invokes its
addTab() method to pass in the relevant information (including the
result of the body evaluation)
(4) [Behavioral] In its own doEndTag(), myTab:Tabs writes out
appropriate code based on all the data passed to its addTab() method
since the last doStartTag() invocation.

You really should read up on all of this in the JSP specification (my
choice) or a suitable reference book. Custom tags aren't that *** ce
you get the hang of them, but you can really bring trouble on yourself
if you don't follow all the details of the behavi *** contracts that
exist between tag handlers and their container.

John Bollinger