aspose file tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why doInitBody Not invoked? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "Why doInitBody Not invoked?" Watch "Why doInitBody Not invoked?" New topic
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
    
  11

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!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Why doInitBody Not invoked?
 
Similar Threads
doAfterBody() is not executed using BodyTagSupport
Custom Tag with Body (addition of body content)
Mock Exam Question - related to doAfterBody
BodyTagSupport return values
Tag Handler - how to capture body of a tag???