Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BodyContent

 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is the tagHandlerClass you have posted is what you are trying to execute ??? or you have posted an incomplete class ??

avi sinha
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's everything that I have.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't override doStartTag, the body will be buffered, so nothing will be printed out.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I ask for help? I really appreciate Your help!
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Christophe said, you need to write the content of the body yourself. You can do it in the doAfterBody like this
 
Niteen Patil
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
avi sinha.

 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I attach it once again:

BodyHandler.java



myTLD.tld



page.jsp

 
Niteen Patil
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitin if you write something by mistake, then you can edit your post using the button...
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic