I’m trying to use a ClasspathResourceLoader for loading my *.vm files. I have them in /WEB-INF/templates, which I’ve added to the build path. My web.xml has:
<servlet>
<servlet-name>ServletVelocity</servlet-name>
<servlet-class>com.edw.servlet.VelocityServlet</servlet-class>
<init-param>
<param-name>org.apache.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
The velocity.properties file is placed in the WEB-INF folder. The key/values I need are:
resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
However, when I try:
template = Velocity.getTemplate("index.vm");
I get:
Exception caught: Unable to find resource 'index.vm'
I’ve read that this could happen if my ResourceLoader failed, but I’ve specified it in the properties file to be the correct one, which works.
By default, Velocity uses a velocity.properties file located inside the jar. If I edit that one (the two aforementioned keys/values), everything works. My presumption would be that the velocity.properties mentioned in my web.xml fails to load, however I can see this in my console as I run the servlet:
INFO: Velocity [trace] Searching for properties at: /WEB-INF/velocity.properties
INFO: Velocity [debug] Configuring Velocity with properties at: /WEB-INF/velocity.properties
...
INFO: Velocity [debug] Initializing Velocity, Calling init()...
INFO: Velocity [debug] Default Properties File: org\apache\velocity\runtime\defaults\velocity.properties (???)
INFO: Velocity [debug] ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
INFO: Velocity [trace] ClasspathResourceLoader : initialization complete.
BUT, if I edit the default velocity.properties file inside the velocity jar (org\apache\velocity\runtime\defaults\velocity.properties), everything works just fine, I can load the .vm okay: no errors.
Files in /WEB-INF/templates are not in class path, I’m not sure about Velocity but Spring’s ClassPathResourceLoader can’t find files in that folder, you must use a web context resource loader then.