通过 web.xml 的速度属性文件
我正在尝试使用 ClasspathResourceLoader 来加载我的 *.vm 文件。我将它们放在 /WEB-INF/templates 中,并将其添加到构建路径中。我的 web.xml 有:
<servlet>
<servlet-name>ServletVelocity</servlet-name>
<servlet-class>com.edw.servlet.VelocityServlet</servlet-class>
<init-param>
<param-name>org.apache.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
velocity.properties 文件放置在 WEB-INF 文件夹中。我需要的键/值是:
resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
但是,当我尝试时:
template = Velocity.getTemplate("index.vm");
我得到:
Exception caught: Unable to find resource 'index.vm'
我已经读到,如果我的 ResourceLoader 失败,可能会发生这种情况,但我已在属性文件中将其指定为正确的,可以正常工作。
默认情况下,Velocity 使用位于 jar 内的velocity.properties 文件。如果我编辑那个(前面提到的两个键/值),一切都会正常。我的假设是,我的 web.xml 中提到的velocity.properties无法加载,但是当我运行servlet时,我可以在控制台中看到这一点:
INFO: Velocity [trace] Searching for properties at: /WEB-INF/velocity.properties
INFO: Velocity [debug] Configuring Velocity with properties at: /WEB-INF/velocity.properties
...
INFO: Velocity [debug] Initializing Velocity, Calling init()...
INFO: Velocity [debug] Default Properties File: org\apache\velocity\runtime\defaults\velocity.properties (???)
INFO: Velocity [debug] ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
INFO: Velocity [trace] ClasspathResourceLoader : initialization complete.
但是,如果我编辑velocity jar中的默认velocity.properties文件(org \apache\velocity\runtime\defaults\velocity.properties),一切正常,我可以正常加载 .vm:没有错误。
I'm trying to use a ClasspathResourceLoader for loading my *.vm files. I have them in /WEB-INF/templates, which I've added to the build path. My web.xml has:
<servlet>
<servlet-name>ServletVelocity</servlet-name>
<servlet-class>com.edw.servlet.VelocityServlet</servlet-class>
<init-param>
<param-name>org.apache.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
The velocity.properties file is placed in the WEB-INF folder. The key/values I need are:
resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
However, when I try:
template = Velocity.getTemplate("index.vm");
I get:
Exception caught: Unable to find resource 'index.vm'
I've read that this could happen if my ResourceLoader failed, but I've specified it in the properties file to be the correct one, which works.
By default, Velocity uses a velocity.properties file located inside the jar. If I edit that one (the two aforementioned keys/values), everything works. My presumption would be that the velocity.properties mentioned in my web.xml fails to load, however I can see this in my console as I run the servlet:
INFO: Velocity [trace] Searching for properties at: /WEB-INF/velocity.properties
INFO: Velocity [debug] Configuring Velocity with properties at: /WEB-INF/velocity.properties
...
INFO: Velocity [debug] Initializing Velocity, Calling init()...
INFO: Velocity [debug] Default Properties File: org\apache\velocity\runtime\defaults\velocity.properties (???)
INFO: Velocity [debug] ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
INFO: Velocity [trace] ClasspathResourceLoader : initialization complete.
BUT, if I edit the default velocity.properties file inside the velocity jar (org\apache\velocity\runtime\defaults\velocity.properties), everything works just fine, I can load the .vm okay: no errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
/WEB-INF/templates 中的文件不在类路径中,我不确定 Velocity,但 Spring 的 ClassPathResourceLoader 无法在该文件夹中找到文件,那么您必须使用 Web 上下文资源加载器。
Files in /WEB-INF/templates are not in class path, I'm not sure about Velocity but Spring's ClassPathResourceLoader can't find files in that folder, you must use a web context resource loader then.
当您调用 Velocity.doanything 时,您是在要求 Velocity 单例执行此操作。您的速度属性正在配置 VelocityServlet,而不是 Velocity 单例。
查看 VelocityTools 项目中的一些示例 servlet 应用程序。在那里您将找到一个未被弃用的高级 servlet,以及如何在 servlet 环境中使用 Velocity 的一些示例。
When you call Velocity.doanything, you are asking the Velocity singleton to do it. Your velocity properties are configuring the VelocityServlet, not the Velocity singleton.
Look at some of the example servlet apps in the VelocityTools project. There you will find a superior servlet that is not deprecated and some examples of how Velocity is used in a servlet environment.