tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nedwick, Robert" <Robert.Nedw...@B2eMarkets.com>
Subject RE: Tags creating Tags
Date Mon, 27 Oct 2003 13:09:24 GMT
> -----Original Message-----
> From: Lukas Bradley [mailto:lukas@somnia.com]
> > I think you can accomplish what you're looking for using
> JSP 2.0 and
> > tag files.
> 
> Yep. That's exactly what I'm looking for. Now Tomcat 5 just
> needs to be released, because I'm stuck with JSP 1.2 for now. *sniff*
> 

Not sure if this will help or not, but it is possible to do something
similar to the JSP 2.0 tag fragment stuff with your own custom tags using
the RequestDispatcher.include() method.  

For our application (JSP 1.2/Servlet 2.3 app), we had a situation where we
wanted to have a custom tag that was capable of rendering a list of data
elements, where each element in the list could be a different type and have
a different HTML representation.  

We didn't really want to embed all the HTML into "out.println()" statements
into one or more tags since HTML in Java is hard to read.  We did want to
use a custom JSP tag though, since the output for this list of elements was
done in several places in the application, so it was impractical to embed
the HTML into every page directly with JSTL <c:when> blocks.

What we did was write a tag similar to:

<ui:renderData var="data" />

The renderData tag would do some common processing, and then lookup a JSP
fragment to render (based on the type of the "data" element).  Inside the
tag, we would then use RequestDispatcher.include() to include the JSP
fragment that we wanted to use.  This allowed us to abstract out the logic
of determining which fragment to use to the Java code in the tag, as well as
allow us to perform any logic common to all cases.  But, we still retained
the flexibility to dynamically embed an arbitrary JSP fragment as well as
keep the unique HTML formatting for each type of data in JSP, rather than a
bunch of ugly out.println statements in the JSP tag.

If you wanted to pass data to the JSP fragment through the
RequestDispatcher.include call, you'll need to set request attribute
variables in the JSP tag to pass data along since the pageContext will be
empty when you get to the JSP fragment.

Also, FYI, if you do this you'll need to buffer the output from the
RequestDispatcher.include call by passing a subclass of
HttpServletResponseWrapper that has all data written to an in memory buffer
as the response object.  After the include call, you'd fetch the data buffer
from your response wrapper object and then write it manually to the
JspWriter.  See the JSTL <c:import /> implementation for details.  We had to
do this because some JSP containers that we run under would end up with data
written to the response stream out of order as a result of this strategy.

Hope this is clear and helps you out, 

Robb

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Mime
View raw message