我正在尝试使用ClasspathResourceLoader加载我的*.vm文件。我将它们放在/WEB/模板中,并将其添加到构建路径中。我的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文件夹中。我需要的关键/价值是:
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失败,这可能会发生,但是我在属性文件中指定它是正确的,这是有效的。
默认情况下,速成使用位于jar中的velocity.properties文件。如果我编辑这一个键(上面提到的两个键/值),一切都正常。我的假设是,我的web.xml中提到的web.xml无法加载,但是在运行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.但是,如果我在速度jar (org\apache\velocity\runtime\defaults\velocity.properties),中编辑默认的velocity.properties文件--一切正常工作--我可以加载.vm okay:没有错误。
发布于 2012-01-31 15:50:35
/WEB-INF/templates中的文件不在类路径中,我不确定速度,但是Spring的ClassPathResourceLoader无法在该文件夹中找到文件,因此必须使用web上下文资源加载器。
发布于 2012-01-31 15:45:02
当您调用Velocity.doanything时,您是在要求速度单例程序这样做。您的速度属性配置的是VelocityServlet,而不是速度单例。
查看VelocityTools项目中的一些servlet应用程序示例。在这里,您将看到一个高级servlet,它是不受欢迎的,以及在servlet环境中如何使用速度的一些例子。
https://stackoverflow.com/questions/9080810
复制相似问题