aspose file tools*
The moose likes Servlets and the fly likes Tag Libraries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Tag Libraries" Watch "Tag Libraries" New topic
Author

Tag Libraries

Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Hai All!
This question is on Tag Libraries. I Have following code in my JSP page.
<Test:ForLoop objTagParamter="<%=vctrArrAccountBeans%>">

<Test:Retrieve objTagParameter="<%=vctrArrAccountBeans.elementAt(((Integer)pageContext.getAttribute(\"INDEX\")).intValue())%>">
<jsp:include page="/ConstructAccountSummary">
<jsp aram name="CardName" value="<%=(String)pageContext.getAttribute(\"CARDNAME\")%>"/>
<jsp aram name="Secure" value="<%=(String)pageContext.getAttribute(\"SECURE\")%>"/>
<jsp aram name="AccountId" value="<%=(String)pageContext.getAttribute(\"ACCOUNTID\")%>"/>
</jsp:include>
</Test:Retrieve>

</Test:ForLoop>

Functions of Tags: ForLoop to iterate for times equal to vector size.
Retrieve is to get data from n th element of vector.
and i am using jsp:include Tag for invoking a servlet which displays n th item of vector to browser(Printwriter).
PROBLEM is Only first element is getting printed in Browser.
In both For and Retrieve Tag Handler doAfterBody methods i had following code probably the one that is creating problem.
BodyContent objBody = getBodyContent();
objBody.writeOut(getPreviousOut());
objBody.clearBody();
(One more point.. If i kept JSP:include tags outside the Retrieve Tag it is working fine but i donn wann in that way...)
Any help in this direction is appreciable.
TIA
Rgds
Manohar
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Could we see the code for the tag too?
Thanks
Simon
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Hai Simon!
Thanks for replying. But what part of code in Tag and which Tag would u like to see?
Rgds
Manohar
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Let's start with the ForLoop tag...
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
For Loop Tags doStart and doAfterBody method codes
public int doStartTag()
{
System.out.println("FOR-->inside doStartTag");
if(iterator == null)
{
return SKIP_BODY;
}
if(iterator.hasMoreElements())
{
pageContext.setAttribute("INDEX",new Integer(m_index++));
return EVAL_BODY_TAG;
}
else
{
return SKIP_BODY;
}
}

public int doAfterBody() throws JspTagException
{
System.out.println("FOR-->inside doAfterBody");
BodyContent body = getBodyContent();
try
{
System.out.println(getPreviousOut());
body.writeOut(getPreviousOut());
body.clearBody();
}
catch (IOException e)
{
throw new JspTagException("IterationTag: " +e.getMessage());
}

if (iterator.hasMoreElements())
{
pageContext.setAttribute("INDEX",new Integer(m_index++));
System.out.println("FOR-->doAfterBody-->In Loop-->"+iterator.nextElement());
// this instructs the jsp engine to execute atleast one more call of the doAfterBody()
if(iterator.hasMoreElements())
return EVAL_BODY_TAG;
else
{
return SKIP_BODY;
}

}
else
{// tells the jsp engin e to stop processing the body of the tag
return SKIP_BODY;
}
}
Thanks
Manohar
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Okay, I'm assuming that you are using BodyTag and not the new IterationTag here. If so, I would be tempted to move the code that outputs the body content into the doEndTag method. For example...
public int doEndTag() throws JspException {
try {
if (bodyContent != null) {
bodyContent.writeOut(getPreviousOut());
}
} catch (IOException ioe) {
throw new JspException(ioe.getMessage());
}
return EVAL_ PAGE;
}
Let us know if that makes any difference,
Cheers
Simon
[This message has been edited by Simon Brown (edited August 29, 2001).]
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Of course instead of fixing this problem, we could refactor your two tags into a single tag.
Basically, in the doStartTag and doAfterBody methods of your ForLoop tag, instead of inserting the "index" back in to the page, you could just insert the object returned by nextElement() instead. This means that you've cut down the complexity associated with your JSP as you could remove the Retrieve tag.
There are plenty of examples around, including one on the Sun Java site ( http://java.sun.com/j2ee/tutorial/doc/JSPTags.html ).
Also, there are some examples that I presented at JavaOne this year may be of use ( http://www.simongbrown.com - BOF-1791 in the JavaOne section).
Hope that helps
Simon
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Hai Simon!
Thanks! It worked fine... Regarding ur second suggestion it's nice but for my requirement it won't work!
I need to have generic for Loop handler and I need to execute jsp:include tag only under certain conditions which i am checking in Retrive Tag...
Once Again Thanks for ur help..
Rgds
Manohar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tag Libraries