On Sat, 9 Dec 2000, James Carman wrote:
> Does Tomcat support the JSP body content for a BodyTag? In my doAfterBody
> method (inherited from BodyTagSupport), I return EVAL_BODY_TAG. I have some
You must return EVAL_BODY_TAG in your doStartTag() method, not
doAfterBody(). Have a look at the jakarta-taglibs project for some nice
examples, in particular
/utility/src/org/apache/taglibs/utility/basic/IncludeTag.java
As an aside, I noticed that JRun does not comply with the spec and call
setBodyContent on empty tags like <util:include url="foo.html"/>. You have
to trick it by saying <util:include url="foo.html"></util:include>. This
means that some jakarta-taglibs taglibs won't work under JRun. This isn't
fixed in SP1, so watch out for it if you ever have to port to JRun.
--Jeff
> debug statements in my code, so I can see that the doAfterBody method is
> called multiple times, but I'm not seeing the html that should be generated
> by my JSP code within the tag's body. Can somebody help me?
>
> ************************************************
> Here's my Java code...
>
> package ws.carman.taglib;
>
> import javax.servlet.jsp.tagext.BodyTagSupport;
> import java.util.Iterator;
> import java.util.Collection;
> import javax.servlet.ServletRequest;
>
> public class IterateTag extends BodyTagSupport
> {
> private String m_CollectionName;
> private String m_ElementName;
> private Iterator m_Iterator;
>
> public final void setCollectionName( final String newCollectionName )
> {
> m_CollectionName = newCollectionName;
> }
>
> public final void setElementName( final String newElementName )
> {
> m_ElementName = newElementName;
> }
>
> public final int doStartTag()
> {
> final ServletRequest request = pageContext.getRequest();
> final Collection coll = ( Collection )request.getAttribute(
> m_CollectionName );
> m_Iterator = coll.iterator();
> return doNext();
> }
>
> public final int doAfterBody()
> {
> System.out.println( "Inside doAfterBody!" );
> return doNext();
> }
>
> private final int doNext()
> {
> if( m_Iterator.hasNext() )
> {
> pageContext.getRequest().setAttribute( m_ElementName,
> m_Iterator.next() );
> System.out.println( "Current element is " +
> pageContext.getRequest().getAttribute( m_ElementName ) );
> return EVAL_BODY_TAG;
> }
> else
> {
> return SKIP_BODY;
> }
> }
>
> public void release()
> {
> m_ElementName = null;
> m_CollectionName = null;
> m_Iterator = null;
> }
> }
>
> *********************************************************
> Here's the tag library descriptor file...
>
> <?xml version="1.0" encoding="ISO-8859-1" ?>
> <!DOCTYPE taglib
> PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
> "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
>
> <!-- a tag library descriptor -->
>
> <taglib>
> <tlibversion>1.0</tlibversion>
> <jspversion>1.1</jspversion>
> <shortname>carman</shortname>
> <uri></uri>
> <info></info>
> <bodycontent>JSP</bodycontent>
> <tag>
> <name>iterate</name>
> <tagclass>ws.carman.taglib.IterateTag</tagclass>
> <info>Iterates throug a named collection</info>
>
>
> <attribute>
> <name>collectionName</name>
> <required>true</required>
> </attribute>
>
> <attribute>
> <name>elementName</name>
> <required>true</required>
> </attribute>
> </tag>
> </taglib>
>
> ***********************************************************
> And finally, here's my JSP...
>
> <HTML>
> <HEAD>
> <TITLE>Iterator Example</TITLE>
> </HEAD>
> <%@ page import="java.util.*" %>
> <%@ taglib uri="taglib/iterate.tld" prefix="carman" %>
>
> <%
> final String[] listElements = new String[] { "Hello", "World", "How",
> "Are", "You" };
> final List list = Arrays.asList( listElements );
> request.setAttribute( "list", list );
> %>
> <BODY>
> Before Tag!<BR>
> <carman:iterate collectionName="list" elementName="element">
> The current element is <%=request.getAttribute( "element" )%><BR>
> </carman:iterate>
> After Tag!<BR>
> </BODY>
> </HTML>
>