Velocity Not Finding Template

走远了吗. 提交于 2019-12-13 20:43:12

问题


I am trying to get Apache Velocity up and running. I have a TestClass.class class in my my.test.package package.

public class TestClass {
    public static Template getTestTemplate() throws Exception {
        Velocity.init();
        return Velocity.getTemplate("MyTestTemplate.vm");
    } 
}

In the same location (my.test.package) I have the MyTestTemplate.vm file.

The above code causes an exception to be thrown, saying Unable to find resource 'MyTestTemplate.vm'. I am not sure what the issue is. Does Velocity not look for the file in the same package? (Note: I originally had the file in the resources folder, but put it under the same folder for testing purposes).


回答1:


Okay, figured it out.

So I figured maybe Velocity was looking in my WEB-INF/classes folder. I looked in there only to discover that the MyTestTemplate.vm file wasn't there. Turned out I needed to update my Ant script that copied over the resources to include .vm files.

<include name="**/*.vm"/>

Then I needed to update my configuration so that Velocity knew to look in the classes folder.

VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty("resource.loader", "class");
velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
velocityEngine.init();

Then when you get the template, you just need to provide the path after the WEB-INF/classes part.

velocityEngine.getTemplate("path/to/resource/MyTestTemplate.vm");

I am sure there is some way to get the templates off of the file path, but I stopped caring ;)



来源:https://stackoverflow.com/questions/11125236/velocity-not-finding-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!