velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "bluejoe" <>
Subject Re: question on Velocity parsing
Date Thu, 25 Dec 2008 02:41:38 GMT
Thanks for your help, Mr. Nathan Bubna.

In my project I need define a new class extending from VelocityViewServlet so that createContext()
and getVelocityEngine() methods are visible. See belows:

public class MyServlet extends VelocityViewServlet
    public VelocityEngine getVelocityEngine()
        return super.getVelocityEngine();

    public Context createContext(HttpServletRequest arg0,
            HttpServletResponse arg1)
        return super.createContext(arg0, arg1);

Then I use MyServlet as a singleton tool to create contexts and help to render template pages.
However, I can't use VelocityLayoutServlet directly, because the layout template maybe uses
another layout. For solving this problem, I write a method called layout() as follows:

    public void layout(String templatePath, Context ctx, Writer writer)
            throws Exception
        StringWriter sw = new StringWriter();

        render(templatePath, ctx, sw);

        String layout = (String) ctx.get("layout");
        if (layout != null)
            ctx.put("body", sw.toString());
            layout(layout, ctx, writer);

I traced into the source codes and make sure that the cache works but the parse() method costs
lots of time. I think it is caused by the complexity of templates as following:

1. Templates use layout recursively;
2. Templates use many #parse directives to include other templates, such as headers and footbars,
and so on;

How is your idea?

Merry Chistmas!


----- Original Message ----- 
From: "Nathan Bubna" <>
To: "Velocity Users List" <>
Sent: Thursday, December 25, 2008 1:39 AM
Subject: Re: question on Velocity parsing

> First, you should not be using VelocityServlet, but instead use
> VelocityViewServlet.  As for parsing files more quickly, there is only
> one parsing method available at this time.  You are welcome to try
> serializing them, though i'm not aware of any effort to enable or aid
> that process.  It may not be workable.
> Just be sure you have caching on so that you only have to incur the
> parsing time once and use the latest version of Velocity (1.6).  Also,
> try to simplify your templates if you still need more speed.  I'm
> afraid that's about all that can be done right now.
> On Tue, Dec 23, 2008 at 1:28 AM, bluejoe <> wrote:
>> Hi, all,
>>    Now I use Velocity and VelocityServlet to generate template files (just like JSP
pages) according to model template files, but I find the first-time loading cost much time.
Can you tell me how to parse Velocity templates more quickly? Or is it ok to serailize parsed
templates (It seems that they will be parsed as SimpleNodes) into files? I need your help.
>>    Thanks a lot!
>> bluejoe
>> 2008-12-23
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:
View raw message