File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Custom Tag with Body (addition of body content) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Custom Tag with Body (addition of body content)" Watch "Custom Tag with Body (addition of body content)" New topic
Author

Custom Tag with Body (addition of body content)

Kumar Jaya
Ranch Hand

Joined: Jan 12, 2009
Posts: 45
I was trying the example in the sun site 'http://java.sun.com/developer/technicalArticles/xml/WebAppDev3/' I have some problem trying to understand the last example 'Sample 13: LoopTag.java' which prints the tag's body to the times given in the tag's attribute 'times'.

The example seems to work fine, but I am not able to understand the flow of it


The corresponding JSP page



I am sure that the setBodyContent(BodyContent bodyContent) method is called only once and doAfterBody() is called 4 times, but I am not sure where the addition to the BodyContent is made. I just include a sysout in the doAfterBody() to examine the contents of BodyContent

i.e.,

The first call to doAfterBody() prints

"Welcome to Custom Tags Programming.<BR>"

and the second call to doAfterBody() prints

"Welcome to Custom Tags Programming.<BR>"
"Welcome to Custom Tags Programming.<BR>"

and so on..

How does this occur in the background without the BodyContent variable being altered any where in the code and setBodyContent() method being called??

Thanks for the help
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
The addition to the body happens in the standard part of the tag lifecycle.
I would suggest you go and read the API Docs on the body tag to find out about its lifecycle. Basically the tag is designed so that it will do work behind the scenes, and let you have control at certain points in the lifecycle.

The tag executes the body (as part of its lifecycle) which writes to the BodyContent object, and then calls the doAfterBody() method do find out what you want to do next.
The bodyContent variable itself doesn't change, but its not immutable. Its contents can be written to - which is what the "DO BODY" part of the lifecycle would do.


Also note that you are using constants that don't exactly fit with the Body tag.
EVAL_BODY_TAG has been deprecated. You should instead use EVAL_BODY_BUFFERED (has the same constant value, so it currently works).

The doAfterBody method should return EVAL_BODY_AGAIN rather than EVAL_BODY_TAG.
I guess thats just a really old article?

Kumar Jaya
Ranch Hand

Joined: Jan 12, 2009
Posts: 45
I went through the API docs of the BodyTag and BodyContent, The lifecycle of BodyTag is as follows "doStartTag(), setBodyContent(), doInitBody(), BODY, doAfterBody()". BodyContent is a subclass of JspWriter, so when a BODY lifecycle occurs can I assume the contents of the action are appended to the previous content?

The API for the BodyContent says "An encapsulation of the evaluation of the body of an action so it is available to a tag handler.", So I think evaluation of a Body results in content addition(also).

Stefan Evans wrote:The bodyContent variable itself doesn't change, but its not immutable. Its contents can be written to - which is what the "DO BODY" part of the lifecycle would do.


Also note that you are using constants that don't exactly fit with the Body tag.
EVAL_BODY_TAG has been deprecated. You should instead use EVAL_BODY_BUFFERED (has the same constant value, so it currently works).

The doAfterBody method should return EVAL_BODY_AGAIN rather than EVAL_BODY_TAG.
I guess thats just a really old article?


Yes the bodyContent variable itself doesn,t change, but how does the framework have access to my bodyContent variable (which can private also) if it has to change the contents? does it use some sort of reflection mechanism?

Its an old code from sun site. I am just trying to figure out the mechanism behind the scenes. I appreciate the reply, thanks.
 
Don't get me started about those stupid light bulbs.
 
subject: Custom Tag with Body (addition of body content)
 
Similar Threads
JSP-Servlet
Custom Tag Iterator is so slow.
BodyTagSupport return values
Tag Handler - how to capture body of a tag???
Why doInitBody Not invoked?