速度找不到模板 [英] Velocity Not Finding Template
问题描述
我正在尝试启动并运行Apache Velocity.我的my.test.package
包中有一个TestClass.class
类.
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");
}
}
在同一位置(my.test.package
),我有MyTestTemplate.vm
文件.
In the same location (my.test.package
) I have the MyTestTemplate.vm
file.
上面的代码导致抛出异常,表示为Unable to find resource 'MyTestTemplate.vm'
.我不确定是什么问题. Velocity不会在同一软件包中查找文件吗? (注意:我最初在资源文件夹中有该文件,但是出于测试目的,将其放在同一文件夹下).
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).
推荐答案
好,知道了.
所以我想也许Velocity正在查看我的WEB-INF/classes
文件夹.我到那里看时才发现MyTestTemplate.vm
文件不存在.原来,我需要更新在资源上复制的Ant脚本,以包含.vm
文件.
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"/>
然后我需要更新配置,以便Velocity知道要在classes
文件夹中查找.
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();
然后,当您获得模板时,只需在WEB-INF/classes
部分之后提供路径.
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 ;)
这篇关于速度找不到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!