sunleo
sunleo

Reputation: 10947

velocity template resource not found exception

Exception occurred: org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'ResourceLoader1.vm'

I am having ResourceLoader1.vm in /WEB-INF/templates, I stuck up with this please help me.

            Properties props = new Properties();  
            props.put("file.resource.loader.path", "/WEB-INF/templates");
            Velocity.init(props);
            VelocityContext context = new VelocityContext();
            context.put("greetings", "Many Happy returns");
            context.put("name", "Joseph");
            Writer writer = new StringWriter();
            Template template = Velocity.getTemplate("ResourceLoader1.vm");
            template.merge(context, writer);

Upvotes: 1

Views: 9568

Answers (2)

Ridcully
Ridcully

Reputation: 23655

It seems you're using Velocity in a web application. For this, you'd be better off using the VelocityViewServlet that's specifically designed for this kind of usage.

The FileResourceLoader that's used with your configuration has no knowledge of the webserver and contexts and stuff, so the way you've configured it, it would look for a WEB-INF folder at the root of the filesystem your application server is running at.

Upvotes: 3

duffymo
duffymo

Reputation: 308733

You should put .vm templates relative to the CLASSPATH. A better choice would be to put the /templates directory under WEB-INF/classes, lose the props, and fetch it like this:

Template template = Velocity.getTemplate("templates/ResourceLoader1.vm");

Upvotes: 4

Related Questions