Velocity Template Engine - ResourceNotFound Exception
I've only used this template engine for a short time but I'm already inloved with it. We use Velocity to generate e-mail templates and I happen to encounter an error when I was trying to configure it progmatically:
Properties p = new
Properties() ;
p.setProperty("resource.loader","file");
p.setProperty("file.resource. loader.class",
"org.apache.velocity .runtime. resource. loader.FileResou rceLoader");
p.setProperty("file.resource. loader.path",
"C:/opt/templates/");
p.setProperty("file.resource. loader.cache", "false");
p.setProperty("file.resource. loader.modificat ionCheckInterval",
"0");
Velocity.init( p);
VelocityEngine ve =
new VelocityEngine( );
ve.init();
Template t =
ve.getTemplate("my_template. vm");
VelocityContext
context = new VelocityContext( );
t.merge( context,
writer );
I would encounter an error
org.apache.velocity .exception. ResourceNotFound Exception: Unable to find
resource 'my_template. vm'
I'm surpirsed it took me about 2 or 3 days to figure out that there are actually 2 ways of using the Velocity engine. Singleton and separate instance model. So the above code could've been fine if I changed the lines
VelocityEngine ve =
new VelocityEngine( );
ve.init();
Template t =
ve.getTemplate("my_template. vm");
VelocityContext
context = new VelocityContext( );
t.merge( context,
writer );
with
Template t = Velocity.getTemplate("my_template. vm");
VelocityContext
context = new VelocityContext( );
t.merge( context,
writer );
I hope this helps a lot of people. :)