Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About BodyTag/BodyTagSupport

 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have three questions, if you don't mind.
(1)
I tried creating a simple class which extends BodyTagSupport. This class has the following methiods:
- doStartTag()
- doEndTag()
- release()
- doInitBody()
- doAfterBody()
I tried to insert this lines of code to each of the methods above:
try {
JspWriter out = pageContext.getOut();
out.println("any text<br>");
} catch (IOException ioe) {}
How come the body-related functions (doInitBody and doAfterBody) were not able to display anything using JspWriter? How come only doStartTag, doEndTag and release are able to display using JspWriter?
(2)
Another question, how come the following code does not display anything on the browser. I thought BodyContent is derived from JspWriter?
BodyContent body = getBodyContent();
body.println("any text");
(3)
Is there a way to get BodyContent object inside doStartTag(), doEndTag() and release()? Or are they just valid (existing) inside doAfterBody()?
Thank you very much.
More power.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marlon tan:
How come the body-related functions (doInitBody and doAfterBody) were not able to display anything using JspWriter? How come only doStartTag, doEndTag and release are able to display using JspWriter?
Oh, but they are, it may just not be the JspWriter you think it is . Remember, the BodyTag is for situations where you want to capture the body output and perform some transformation on it. The body is therefore not directly written to the jsp output stream, it's up to you to decide what to do with it. I bet you're just throwing the body content away!
Investigate BodyTagSupport.getPreviousOut(). Also, read the spec and the BodyTag javadoc for the tag invocation sequence, paying special attention to when exactly JspWriters are pushed and popped.
HTH
- Peter
 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the help, Peter
Would anybody know how come the code below doesn't display any text on the browser? How come the first time I tried JspWriter, it did display something, and now it doesn't??
Thanks
-----
public int doAfterBody() throws JspTagException {
System.out.println("InnerTag.doAfterBody():");
for (int i=startCol; i<=endCol; i++) {
String output = rowNum + " x " + i + " = " + rowNum*i;
System.out.println(output);
try {
JspWriter out = pageContext.getOut();
out.println("Hello<br>");
} catch (Exception ioe) {
System.out.println("Error in doAfterBody() - JspWriter: "
+ ioe + "<br>");
}
}
return SKIP_BODY;
} // doAfterBody()

[This message has been edited by marlon tan (edited October 19, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic