You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by bu...@apache.org on 2003/10/06 16:50:03 UTC

DO NOT REPLY [Bug 23621] New: - Modify method mergeTemplate() in VelocityViewServlet and perhaps VelocityLayoutServlet

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=23621>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=23621

Modify method mergeTemplate() in VelocityViewServlet  and perhaps VelocityLayoutServlet 

           Summary: Modify method mergeTemplate() in VelocityViewServlet
                    and perhaps VelocityLayoutServlet
           Product: Velocity
           Version: 1.4
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Tools
        AssignedTo: velocity-dev@jakarta.apache.org
        ReportedBy: payne@telerx.com


Modify the mergeTemplate() method of the VelocityViewServlet 
To use 
PrintWriter pw = response.getWriter(); 

And use the PrintWriter instead of the ServletOutputStream.

Refer to: 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.getWriter(), lets be compatible with them

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

Why---> 
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(can be included in jsp's). 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>

Here is the message from the user list.

http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg10683.html

Note though, this change will not completely help the opposite, where a
definition is based on a velocity layout.

i.e.

 <definition name="vmlayout"
 	            path="/layout.vm">
>> 		<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>

In order for that to work, it would require an additional change to the tiles
tool.  I know what needs to be done, but holding that for another discussion.

Here is what I have for my mergeTemplate()


    /**
     *  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
    {
        PrintWriter vw = response.getWriter();

        try
        {
	    template.merge(context, vw);
        }
        finally
        {
            try
            {
                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 */
                }                
            }
            catch (Exception e)
            {
               //Velocity.error("Error merging template \"" + template.getName()
+ "\": " + e);
	      
	       // if there is a problem with the writer, Velocity.error may not work
either.
	         e.printStackTrace(); //instead?
		// do nothing
            }
        }
    }

Evaluate the last try/catch you may of may now want to use Velocity.error

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