Meaningless Drivel is fun!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes BodyContent 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "BodyContent" Watch "BodyContent" New topic
Author

BodyContent

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

I use BodyTagSupport to be able to get a content of a tag. Then I want to store that content in a variable in appscope.



TLD:

And invocation:


Unfortunately - nothing is printed. body variable is empty.
What's wrong?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

is the tagHandlerClass you have posted is what you are trying to execute ??? or you have posted an incomplete class ??

avi sinha


SCJP 5.0 SCWCD 5.0
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

It's everything that I have.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

If you don't override doStartTag, the body will be buffered, so nothing will be printed out.


[My Blog]
All roads lead to JavaRanch
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

I do not want to print out anything. I want to put an attribute in an app scope and after that print it out using EL i my jsp page.

doStartTag() must return EVAL_BODY_BUFFERED (it's default value) so that execution can reach setBodyContent() and doInitBody().

I do not know what's wrong with my code.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Can I ask for help? I really appreciate Your help!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

As Christophe said, you need to write the content of the body yourself. You can do it in the doAfterBody like this


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Niteen Patil
Ranch Hand

Joined: Oct 27, 2009
Posts: 45
I hope the <body-content>tag is set properly in the TLD.(ie its not set to empty).Also , I want to know what is bodyContent in bodyContent .toString().

Thanks.
Hasnain Javed Khan
Ranch Hand

Joined: Nov 23, 2007
Posts: 73
Hi Lucas,

Lucas Smith wrote:Can I ask for help? I really appreciate Your help!


Can you print some statements from doInitBody() method ? See if it's getting called. Can you upload a zip file containing all the required files to run the example ?

Kind Regards.
Hasnain.
Niteen Patil
Ranch Hand

Joined: Oct 27, 2009
Posts: 45
Sorry, I mean't bodyContent.getString() (not bodyContent.toString()).
If it denotes instant of BodyContent class,is it implicitly available in the taghandler class?

Thanks
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

Nitinyanand Patil wrote:is it implicitly available in the taghandler class?

Thanks


yes but only for the classes which extend BodyTagSupport

avi sinha
Niteen Patil
Ranch Hand

Joined: Oct 27, 2009
Posts: 45
Thanks,
avi sinha.

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

OK, I attach it once again:

BodyHandler.java



myTLD.tld



page.jsp

Niteen Patil
Ranch Hand

Joined: Oct 27, 2009
Posts: 45
I just had a look at the api at: http://java.sun.com/javaee/5/docs/api/javax/servlet/jsp/tagext/BodyTag.html#doInitBody().

The api states that:
Prepare for evaluation of the body. This method is invoked by the JSP page implementation object after setBodyContent and before the first time the body is to be evaluated.

According to this ,the body is not evaluated when the doInitBody() is executing.This may be the reason why the body content is not displayed and the appln scoped attribute is not set. If the code would have been in doAfterBody() , the value would have been printed.

Please correct me if I am wrong.
Hasnain Javed Khan
Ranch Hand

Joined: Nov 23, 2007
Posts: 73
Hello Lucas,

Nitinyanand Patil wrote:I just had a look at the api at: http://java.sun.com/javaee/5/docs/api/javax/servlet/jsp/tagext/BodyTag.html#doInitBody().

The api states that:
Prepare for evaluation of the body. This method is invoked by the JSP page implementation object after setBodyContent and before the first time the body is to be evaluated.

According to this ,the body is not evaluated when the doInitBody() is executing.This may be the reason why the body content is not displayed and the appln scoped attribute is not set. If the code would have been in doAfterBody() , the value would have been printed.

Please correct me if I am wrong.


Nitinyanand is correct. The same code below was used in doInitBody() method. Reader's read() method returned -1 for val while in doAfterBody(), it returned the characters "something" one by one.



The code snippet that Ankit provided is wrong. It's a compile time error.


Hope that helped. Thanks Nitinyanand for updating the thread.

Kind Regards.
Hasnain.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Sorry, I didn't test my code before I wrote it, here's the code

But this will not solve the problem that you are facing, the solution given by Nitinyanand is correct, try to set a different value for the attribute (like a simple string instead of bodyContent.getString()) from the doInitBody method and you'll see the output...
Niteen Patil
Ranch Hand

Joined: Oct 27, 2009
Posts: 45
Thanks everyone and special thanks to Lucas Smith for posting such a wonderful thread.
Only by reading the books ,one could not grasped the concept. The discussion helps a lot.

Thanks,
Nitin.
Niteen Patil
Ranch Hand

Joined: Oct 27, 2009
Posts: 45
CORRECTION....
Thanks everyone and special thanks to Lucas Smith for posting such a wonderful thread.
Only by reading the books ,one could not have grasped the concept. The discussion helps a lot.

Thanks,
Nitin.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Nitin if you write something by mistake, then you can edit your post using the button...
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Now everything is clear. Thank you ranchers.
Everything works fine with:



So, I have to remember that the content of a tag is put into the bodyContent after doInitBody() method.
Niteen Patil
Ranch Hand

Joined: Oct 27, 2009
Posts: 45
Nitinyanand Patil wrote:Thanks everyone and special thanks to Lucas Smith for posting such a wonderful thread.
Only by reading the books ,one could not have grasped the concept. The discussion helps a lot.


Thanks Ankit.
 
 
subject: BodyContent
 
Similar Threads
Classic Tags
SimpleTag vs BodyTag
doAfterBody()
BodyTagSupport return values
output of custom tag extending BodyTagSupport class