0

I am using Velocity templates which I store in src/main/webapp/templates.

My Jersey-based REST service needs to find these templates. Locally in my Eclipse (with Sysdeo plugin), I used

p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class",
    "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

to make Tomcat find the templates (which I got from https://stackoverflow.com/a/38812523/927493). Now when I deploy the war to a "real" Tomcat, the resources are not found (Unable to find resource 'templates/FreigabeNeu.vm')

I probably have to set the properties to something different, but I just don't find how I need to do it.

I checked that the templates directory indeed is copied to the main directory of the war.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142

1 Answers1

2

The ClasspathResourceLoader will load resources from the classpath, not just some arbitrary place within a WAR file. That means that if your templates are in /webapp/templates within your WAR file (or wherever) btu that path isn't in the classpath, then they will not be able to be found.

If this is a web application, then what you really want to use is Velocity Tools's WebappLoader which uses the ServletContext to load files from the context itself. That is capable of reading files from virtually anywhere in the web application, even from WAR files, etc.

You might have to double-check to see what the structure of your WAR file – exactly where the templates/ directory ends up – to make sure you have the base path correct. My guess is that they will end up under either /templates or /webapp/templates, but I suspect the former is more likely... the directory webapp is very likely to end up being the root of the WAR file, so /templates is probably correct.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77