tags:

views:

45

answers:

1

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

+2  A: 

You need to initialize Velocity first by calling Velocity.init() (in the singleton model of usage), or VelocityEngine.init() (if you use a separate instance), and passing appropriate configuration parameters. These include the resource loader configuration.

Where to put your template files depends in which resource loader you choose - there are file, classpath, jar, url etc. resource loaders available.

If you use the file resource loader, the template path should be an absolute (directory/file) path. With the jar resource loader, though, it must not be an absolute path (if your templates are within a jar, that is). This is also true for links within your templates, i.e. if one of your templates includes another one by absolute path, the jar resource loader will fail to load it.

Péter Török
Thanks! Not Velocity.init but VelocityEngine#init and it works.
Roman
@Roman, Velocity.init() would work in the singleton model of usage. If you use a separate instance, VelocityEngine.init() is the way indeed. Updated post for clarity :-)
Péter Török