您可以配置速度以使用类路径中的工具箱文件吗?
我将 spring 与 Spring 配置的 VelocityViewResolver 一起使用,
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="order" value="1" />
<property name="suffix" value=".vm" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="toolboxConfigLocation" value="/WEB-INF/velocityToolbox.xml" />
</bean>
如果velocityToolbox.xml 文件位于 WEB-INF 下,则此方法有效;但是我试图将它放在类路径中。有办法做到吗?
我尝试将 toolboxConfigLocation 指定为“classpath:/wherever/velocityToolbox.xml”,但它找不到资源,并且我最终没有配置工具箱,并且在运行时出现 NPE(出于某种原因,看起来像代码期望该位置以“/”开头,或者在查找资源之前添加“/”本身)。
鉴于资源是使用 ServletContext.getResourceAsStream 定位的,toolboxConfigLocation 属性的内容以“/”为前缀,有没有一种方法可以在我的 spring 配置中“定义”一个资源,以某种方式“指向”实际的类路径资源?
欢迎任何想法。
I'm using spring with a VelocityViewResolver configured by Spring
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="order" value="1" />
<property name="suffix" value=".vm" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="toolboxConfigLocation" value="/WEB-INF/velocityToolbox.xml" />
</bean>
This works if the velocityToolbox.xml file is under WEB-INF ; however I'm trying to put it in the classpath. Is there a way to do it ?
I've tried specifying toolboxConfigLocation as "classpath:/wherever/velocityToolbox.xml", but it does not find the resource, and I end up with no toolbox configured, and a NPE at runtime (for some reason, it seems like the code expect the location to start with a '/', or add the '/' itself before looking for the resource).
Given that the resource is located using ServletContext.getResourceAsStream, with the content of the toolboxConfigLocation property prefixed by a "/", is there a way I can "define" a resource in my spring config that would somehow 'point' to an actuall classpath resource ?
Any idea is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信它必须是完整路径,例如:
ServletToolboxManager#getInstance(ServletContext context, String toolboxConfigLocation) 方法用于生成使用工具箱配置的 ToolboxManager,以便创建 VelocityToolboxView 中使用的 ChainedContext。
如果路径不存在,此方法会在路径前附加一个“/”,然后使用 ServletContext#getResourceAsStream(String path) 读取它。
考虑到这一点,如果将其设置为完整路径,就会成功。从上下文根开始的路径。
I believe it has to be a full path, eg:
The ServletToolboxManager#getInstance(ServletContext context, String toolboxConfigLocation) method is used to generate a ToolboxManager configured with the toolbox in order to create the ChainedContext used in the VelocityToolboxView.
This method pre-appends the path with a '/' if one does not exist, and then uses ServletContext#getResourceAsStream(String path) to read it in.
With this in mind, you'll have success if you set it as a full path from the context root.