You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Payne, Matthew" <pa...@Telerx.com> on 2003/10/02 05:25:53 UTC

RE: Mixing jsp content with tiles using tiles tool(possble soluti on!!!!)

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


Re: Mixing jsp content with tiles using tiles tool(possble soluti on!!!!)

Posted by Nathan Bubna <na...@esha.com>.
Payne, Matthew said:
> I found a solution/fix.

cool.  though i wonder how this will affect character encoding issues.  this
is something you should bring up on the velocity-dev list where other
committers will see this.  i'm not gonna move on this without hearing some of
their thoughts.

i'd suggest writing up a quick summary of the problem and your solution and
posting it to the dev list.  you might also create a patch file against the
latest version of VelocityViewServlet (i just checked in some changes to it
today, so be sure you've updated it).  also, when you do that, remove the old
code first, don't just comment it out.

> 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
> ----------------------------------------------------------------------

huh?  don't you mean that jsp calls getWriter()?

>
> 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.
...
> What do you guys think of committing this type of change to the velocity
> tools (i don't have access)????

i'm interested in making this work, but i have my concerns.  i'd like to see
this discussed on velocity-dev first.

> Matthew Payne
...

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