| Author |
Why doInitBody Not invoked?
|
Tiffiny Yang
Ranch Hand
Joined: Mar 29, 2006
Posts: 124
|
|
Here's my snippet. Everything seems fine execept method doInitBody() Not invoked? --- import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class LoopTagAgain extends BodyTagSupport { int times = 0; BodyContent bodyContent; public void setTimes(int times) { this.times = times; } public void doInitBody() throws JspException { try { System.out.println("inside doInitBody().."); this.pageContext.getOut().print("in the doInitBody()"); } catch (IOException e) { throw new JspException(e); } } public int doStartTag() throws JspException { if (times > 0) { System.out.println("in doStartTag() ,see times= " + times ); return EVAL_BODY_INCLUDE; } else { System.out.println("in doStartTag(), time is not bigger than zero, now see times= " + times ); return SKIP_BODY; } } public void setBodyContent( BodyContent bodyContent) { this.bodyContent = bodyContent; } public int doAfterBody() throws JspException { if (times > 1) { times--; System.out.println("in doAfterBody() to see times= " + times ); return EVAL_BODY_AGAIN; } else { return SKIP_BODY; } } public int doEndTag() throws JspException { try { if(bodyContent != null) { System.out.println("I am here..."); bodyContent.writeOut( bodyContent.getEnclosingWriter()); } } catch(IOException e) { throw new JspException("Error: "+e.getMessage()); } return EVAL_PAGE; } } ========= Here's my JSP, and tld file is well defined. <%@ taglib uri="loopTagAgain" prefix="first" %> <HTML> <HEAD><TITLE>Body Tag</TITLE></HEAD> <BODY bgcolor="#aabbcc"> <first:loop times="4"> Welcome to Custom Tags Programming.<BR> </first:loop> </BODY> </HTML> ---------- Thanks in advance !!!
|
 |
Rajitha Arun
Ranch Hand
Joined: Aug 24, 2006
Posts: 43
|
|
Hi, Shouldn't the doStartTag() return EVAL_BODY_BUFFERED to run setBodyContent() and doInitBody()?
|
Rajitha
SCJP, SCWCD
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14669
|
|
|
Yes, as Rajee said, you need to return EVAL_BODY_BUFFER, as doInitBody provides an opportunity to process the buffer before the first evaluation of the body into the buffer.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Tiffiny Yang
Ranch Hand
Joined: Mar 29, 2006
Posts: 124
|
|
Yes, I think you're right, after re-read HFSJ and MZ's note, I knew I was wrong. Thank guys for answering my silly quesiton!
|
 |
 |
|
|
subject: Why doInitBody Not invoked?
|
|
|