my dog learned polymorphism*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes BodyTagSupport return values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "BodyTagSupport return values" Watch "BodyTagSupport return values" New topic
Author

BodyTagSupport return values

Kathiresan Chinna
Ranch Hand

Joined: Aug 17, 2008
Posts: 115
Hi All,

Actually I need to do something in doInitBody() method,
also the body content must be evaluated. So, I use BodyTagSupport.

1. I have a custom tag declared as



2. The class BodyTag1Handler extends BodyTagSupport.

3. In JSP



This is what happening:

1. If doStartTag() return EVAL_BODY_INCLUDE

Body is Evaluated and doAfterBody() is called.
setBodyContent() and doInitBody() is Not called.

This is OK

2. If doStartTag() return EVAL_BODY_BUFFERED

Body is Not Evaluated and doAfterBody() is Not called
But, setBodyContent() and doInitBody() is called.


The Lifecycle described in HFSJ page 563 is not happening in the case 2.

Can someone please tell am I missing anything ?

Thanks in advance
Kathir
Kathiresan Chinna
Ranch Hand

Joined: Aug 17, 2008
Posts: 115
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I don't have the book, so I don't know what the figure looks like. But I'm surprised that doAfterBody() is not called. Really ? Can you show what your tag handler looks like ?


[My Blog]
All roads lead to JavaRanch
Kathiresan Chinna
Ranch Hand

Joined: Aug 17, 2008
Posts: 115
Hi Christophe,

Here is the class

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You're falling into two common pitfalls :

1. You are overriding setBodyContent without setting the BodyContent anywhere. You need to at least call super.setBodyContent, or save the BodyContent into an instance variable. (If you don't use the BodyContent, it's not a problem)

2. The "page context's "out" is being pushed before doAfterBody is called. So when you use JspWriter out = pageContext.getOut();, you are not accessing the page's output stream, but the body buffer stream. That's why nothing is output to your client. You need to either output the body to the original "out", or get it directly with :



Here is a new version of your tag :


[ December 25, 2008: Message edited by: Christophe Verre ]
Kathiresan Chinna
Ranch Hand

Joined: Aug 17, 2008
Posts: 115
aha.. Working fine.

Thank you Christophe
Kathir
 
 
subject: BodyTagSupport return values
 
Similar Threads
doAfterBody() is not executed using BodyTagSupport
Custom tag life cycle
Mock Exam Question - related to doAfterBody
Tag Handler - how to capture body of a tag???
Edurardo Cobian's mock exam answer correct for Question No 25