• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why doInitBody Not invoked?

 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Shouldn't the doStartTag() return EVAL_BODY_BUFFERED to run setBodyContent() and doInitBody()?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic