Velocity can't find resource

48,002

Solution 1

You are using the Webapp resourceloader, which is intended for pages served by the Velocity Tools servlet. (It requires some special initialization to find the root of the servlet context).

I recommend you use the ClasspathResourceLoader, then put the files into WEB-INF/classes, or elsewhere in your classpath. This is really the most straight forward approach.

resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

More info is here:

https://velocity.apache.org/engine/1.7/apidocs/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.html

Solution 2

Will Glass answer is correct, but the configuration should be:

resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

Note the class at the beginning of the second line. See the links provided by him for more details!.

Note: Making an answer instead of a comment due to privileges.

Solution 3

Velocity is probably using the class loader to find those files. I'd recommend putting them in WEB-INF/classes, which is in the CLASSPATH by default.

Share:
48,002
AnAmuser
Author by

AnAmuser

Updated on July 09, 2022

Comments

  • AnAmuser
    AnAmuser almost 2 years

    Something is wrong and it is very frustrating. I read on velocity's homepage that when I run a webapp then some properties should be set. And I've done that but no matter what I do I keep getting the same error.
    This is where I set the props and use velocity

    public class ConfirmationMailGenerator implements MailGenerator {
    
        private BasicUser user;
        private String htmlTemplate = "HTMLConfirmationMailTemplate.vsl";
        private String plainTemplate = "PlainConfirmationMailTemplate.vsl";
    
        public ConfirmationMailGenerator(BasicUser user) {
            this.user = user;
        }
    
        public StringWriter generateHTML() throws Exception {
            Properties props = new Properties();
            props.setProperty("resource.loader", "wepapp");
            props.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.WebappResourceLoader");
            props.setProperty("webapp.resource.loader.path", "/WEB-INF/mailtemplates/");
            VelocityEngine engine = new VelocityEngine(props);
            VelocityContext context = new VelocityContext();
    
            engine.init();
    
            Map map = createDataModel();
            context.put("user", map);
    
            Template template = engine.getTemplate(htmlTemplate);
            StringWriter writer = new StringWriter();
            template.merge(context, writer);
    
            return writer;
        }
    ...
    }
    

    The files is of course saved in /WEB-INF/mailtemplates/.
    If I use this I get this error:

    SEVERE: ResourceManager : unable to find resource 'HTMLConfirmationMailTemplate.vsl' in any resource loader.
    SEVERE: The log message is null.
    

    Thank you for your time:)