Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Why doInitBody Not invoked?

 
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 !!!
 
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()?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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!
 
    Bookmark Topic Watch Topic
  • New Topic