Involving sequence in taglib

J

Jun

I placed a print statement in each callback method in iterating BodyTag and
the print statement prints "Enter ...". I got following

doStartTag

doEndTag

doAfterBody

doAfterBody

doAfterBody

doAfterBody

doAfterBody

My question is why doEndTag was called before DoAfter Body?


Here is the jsp

....
<mylib:loop iterations="5"> </mylib:loop>
....

Here is the definition

....
<tag>
<name>loop</name>
<tagclass>coolshare.tag.LoopTag</tagclass>
<bodycontent>JSP</bodycontent>
<info>A BodyTag for looping</info>
<attribute>
<name>iterations</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
....


And here is the BodyTag java src

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
* A simple BodyTag that Loops a given number of times
* @author Magnus Rydin
*/
public class LoopTag implements BodyTag
{
private Tag parent;
private BodyContent bodyContent;
private PageContext pageContext;
private int iterations=0;

/**
* Constructor
*/
public LoopTag()
{
super();
}

/**
* Method called by the Container to set the PageContext
*/
public void setPageContext(PageContext pageContext)
{
this.pageContext=pageContext;
}

/** Method used by the JSP container to set the parent of the Tag
* @param parent, the parent Tag
*/
public void setParent(final javax.servlet.jsp.tagext.Tag parent)
{
this.parent=parent;
}


/**
* Method used by the JSP container to set the number of iterations to do
*/
public void setIterations(int iterations)
{
this.iterations=iterations;
}

/**
* Method called at start of tag
* @return either a EVAL_BODY_TAG or a SKIP_BODY
*/
public int doStartTag() throws JspTagException
{
print("doStartTag");
if(iterations>0)
{
return EVAL_BODY_AGAIN;
}
else
{
return SKIP_BODY;
}
}

/**
* Method called by the Container to set the BodyContent
*/
public void setBodyContent(BodyContent bodyContent)
{
this.bodyContent=bodyContent;
}

/**
* Method Called before the first time the body is evaluated
*/
public void doInitBody() throws JspTagException{}

/**
* Method called after each evaluation of the body
* @return either EVAL_BODY_TAG or SKIP_BODY
*/
public int doAfterBody() throws JspTagException
{
print("doAfterBody");


if(iterations>1)
{
//decrease the number of iterations left to do
iterations--;
return EVAL_BODY_AGAIN;
}
else
{
return SKIP_BODY;
}
}

/**
* Method Called at end of tag
* @return either EVAL_PAGE or SKIP_PAGE
*/
public int doEndTag() throws JspTagException
{

print("doEndTag");

try
{

if(bodyContent != null) // Check if we even entered the body
bodyContent.writeOut(bodyContent.getEnclosingWriter());

}
catch(java.io.IOException e)
{
throw new JspTagException("IO Error: " + e.getMessage());
}
return EVAL_PAGE;
}

/**
* Method called to releases all resources
*/
public void release() {}

/** Method for retrieving the parent
* @return the parent
*/
public javax.servlet.jsp.tagext.Tag getParent()
{
return parent;
}

private void print(String s)
{
try
{
pageContext.getOut().println("<br>"+s+"<br>");

}
catch(Exception e)
{

}
}
}
 
S

Sudsy

Jun said:
I placed a print statement in each callback method in iterating BodyTag and
the print statement prints "Enter ...". I got following

doStartTag

doEndTag

doAfterBody

doAfterBody

doAfterBody

doAfterBody

doAfterBody

Two questions:
- what servlet container and platform are you using?
- what's the source of the print method?

I modified one of my tags which extends BodyTagSupport to print using
System.err.println (no buffering) and the correct invocation order was
shown in the log. Examination of the generated java code in the work
directory also showed the correct ordering of invocations.
You just might be on a wild goose chase here...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top