• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Custom Tag with Body (addition of body content)

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic