Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity Template Not Found With Classpath

Tags:

java

velocity

I cannot seem to get access to my velocity templates when I switch to the "classpath" resource loader. I've tried putting a templates directory on /WEB-INF/classes/templates, /WEB-INF/templates, creating a templates.jar in /WEB-INF/lib. None of them work. Any ideas? Permissions on those files are all correct.

Properties p = new Properties();
p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.tools.generic.log.CommonsLogLogSystem");

/*
// Works fine:
p.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
p.setProperty("file.resource.loader.path", "/path/to/templates");
*/

// Cannot find template with this:
p.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
p.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
p.setProperty("resourceLoaderPath", "/WEB-INF/classes/templates");

org.apache.velocity.app.Velocity.init(p);

template = org.apache.velocity.app.Velocity.getTemplate("confirmation_html.vm");
like image 281
Bradford Avatar asked Apr 26 '26 19:04

Bradford


1 Answers

This i how i have done it in the past. It has worked, albeit may not be the best way to get it working. Assuming that you have a /webapps/WEB-INF/ structure,

 Properties prop = new Properties();
 String absolutePath=new File(Thread.currentThread().getContextClassLoader().getResource("").getFile()).getParentFile().getParentFile().getPath();//this goes to webapps directory
 prop.put("file.resource.loader.path", absolutePath+"/WEB-INF/classes/templates");
 Velocity.init(prop);
 Template t=Velocity.getTemplate("confirmation_html.vm");

p1ng

like image 150
ping Avatar answered Apr 29 '26 08:04

ping



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!