Where does Velocity search for the template?

0 votes
asked Jul 12, 2010 by roman

I need to use Velocity from Java-code in a web-application (I use it as mail-templates processor).

So, I have a standard code:

VelocityEngine ve = new VelocityEngine ();
try {
   ve.init ();
   Template t = ve.getTemplate (templatePath);
   ...   
} catch (Exception e) {
   throw new MailingException (e);
}

This code always throws the ResourceNotFoundException. Where should I place my templates in web-application (WEB-INF? classpath? etc?) and how should I specify path (i.e. what should I pass as templatePath)?

1 Answer

0 votes
answered Jul 11, 2015 by med116

SETTING UP VELOCITY TEMPLATES

I ended up using this question to solve my problem of setting up the template path. I am using velocity for templating html emails.

Here is a good couple of methods you can use that illustrate how the template path is set up. It sets the property 'file.resource.loader.path' to the absolute path. I created a directory for templates, and then right clicked on the template file to get the full path (In Eclipse). You use that full path as the value of the file.resource.loader.path property. I also added the 'runtime.log.logsystem.class' property, and set it because I was getting exceptions complaining about logging.

UTILTY VELOCITY METHODS

public static VelocityEngine  getVelocityEngine(){

    VelocityEngine ve = new VelocityEngine();
    Properties props = new Properties();
    // THIS PATH CAN BE HARDCODED BUT IDEALLY YOUD GET IT FROM A PROPERTIES FILE
    String path = "/absolute/path/to/templates/dir/on/your/machine";
    props.put("file.resource.loader.path", path);
    props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
    ve.init(props);
    return ve;
}

public static String getHtmlByTemplateAndContext(String templateName, VelocityContext context){

    VelocityEngine ve = getVelocityEngine();

    Template template = ve.getTemplate(templateName);

      StringWriter writer = new StringWriter();
      template.merge(context, writer );
      System.out.println( writer.toString());
      String velocityHtml = writer.toString();
      return velocityHtml;
}

HOW TO USE ABOVE CODE, CREATING A VELOCITY CONTEXT TO FEED TO UTILTY METHOD

Here is how you can use the above methods. You simply specify the filename of the template file, and create a simple VelocityContext context to store your template variables.

        VelocityContext context = new VelocityContext();
        context.put("lastName", "Mavis");
        context.put("firstName", "Roger");
        context.put("email", "mrRogers@wmail.com");
        context.put("title", "Mr.");

        String html =    VelocityUtil.getHtmlByTemplateAndContext("email_template_2015_10_09.vm", context);

EXAMPLE TEMPLATE HTML

The variables can be accessed like (in my case saved in file email_template_2015_10_09.vm):

<p> Hello $firstName $lastName </p>
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...