velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Payne, Matthew" <pa...@Telerx.com>
Subject RE: Mixing jsp content with tiles using tiles tool(possble soluti on!!!!)
Date Thu, 02 Oct 2003 03:25:53 GMT
I found a solution/fix.  Please take a look at this post. 
http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg41885.html

In short -----------------------------------------------------------

you can't call response.getOutputStream() if something else called 
response.getWriter() in the same request.

You will get an IllegalStateExeption.

jsp calls response.getOutputStream(), lets be compatible with them

----------------------------------------------------------------------


In light of this, I modified the mergeTemplate() method in
VelocityViewServlet by 
replacing: 

//ServletOutputStream output = response.getOutputStream();

with

PrintWriter pw = response.getWriter(); (and commented out corresponding
methods)

This appears to work very well.  I tested this on tomcat and resin.

By using response.getWriter(), this simple change would make the
VelocityServlet "more" compatible with jsp.
This should enabled velocity to be have better interchangeability with
tiles/jsp.
Mixed tiles definitions like this will work fine.   

 <definition name="jsplayout"
 	            path="/layout.jsp">
>> 		<put name="header"
>> 		     value="/header.vm"/>
>> 		<put name="footer"
>> 		     value="/footer.jsp"/>
>> 		<put name="body"
>> 		     value="/center/listThemes.vm"/>
>> 		<put name="rail"
>> 		     value="/rail.jsp"/>
>> </definition>

What do you guys think of committing this type of change to the velocity
tools (i don't have access)????

Matthew Payne

Here is a copy of the "hacked" mergeTemplate() below.

  /**
     *  merges the template with the context.  Only
override this if you really, really
     *  really need to. (And don't call us with
questions if it breaks :)
     *
     *  @param template template object returned by
the handleRequest() method
     *  @param context  context created by the
createContext() method
     *  @param response servlet reponse (use this to
get the output stream or Writer
     */
    protected void mergeTemplate(Template template, 
                                 Context context, 
                                 HttpServletResponse
response)
        throws ResourceNotFoundException,
ParseErrorException, 
               MethodInvocationException, IOException,

               UnsupportedEncodingException, Exception
    {
        //ServletOutputStream output = response.getOutputStream();
        PrintWriter pw = response.getWriter();
        VelocityWriter vw = null;
        
        try
        {
           /* vw = (VelocityWriter) writerPool.get();
            
            if (vw == null)
            {
                vw = new VelocityWriter(new
OutputStreamWriter(output, encoding), 4*1024, true);
            }
            else
            {
                vw.recycle(new
OutputStreamWriter(output, encoding));
            } */
           
            template.merge(context, pw);
        }
        finally
        {
            try
            {
               pw.flush();
              
                if (vw != null)
                {
                    // flush and put back into the
pool
                    // don't close to allow us to play
                    // nicely with others.
                    vw.flush();
                    /* This hack sets the
VelocityWriter's internal ref to the 
                     * OutputStreamWriter to null to
keep memory free while
                     * the writer is pooled. See bug
report #18951 */
                    vw.recycle(null);
                    writerPool.put(vw);
                }                
            }
            catch (Exception e)
            {
                // do nothing
            }
        }
    }

-----Original Message-----
From: Nathan Bubna [mailto:nathan@esha.com]
Sent: Wednesday, October 01, 2003 4:54 PM
To: Velocity Users List
Subject: Re: Mixing jsp content with tiles using tiles tool


Payne, Matthew said:
> The filter see its.  Here is the stack trace under tomcat 5.
> Notice the VelocityViewServlet is getting hit. Looks like it would be
> helpful if VelocityViewServlet had a little better error handling.  It
> should probably print a stack trace if it can't write web content.

that's precisely what the VelocityViewServlet.error(...) method was trying
to
do when this exception was thrown.  it apparently was not allowed to print
to
the output stream.

>  [Exception in:/center/listThemes.vm] null java.lang.IllegalStateException
> at
>
org.apache.jasper.runtime.ServletResponseWrapperInclude.getOutputStream(Serv
> letResponseWrapperInclude.java:107) at
> org.apache.velocity.tools.view.servlet.VelocityViewServlet.error(Unknown
> Source) at
>
org.apache.velocity.tools.view.servlet.VelocityViewServlet.doRequest(Unknown
> Source) at
> org.apache.velocity.tools.view.servlet.VelocityViewServlet.doGet(Unknown
...

you might try writing a simple extension of the VVS that logs errors instead
of (or in addition to) printing them to the output stream.  actually, that
might be something worth doing all the time.  i'm trying to remember if
there's some reason it doesn't do that now...

Nathan Bubna
nathan@esha.com


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


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message