您可以配置速度以使用类路径中的工具箱文件吗?

发布于 2024-12-18 19:33:57 字数 926 浏览 2 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2024-12-25 19:33:57

我相信它必须是完整路径,例如:

/WEB-INF/classes/com/myapp/resources/toolbox.xml

ServletToolboxManager#getInstance(ServletContext context, String toolboxConfigLocation) 方法用于生成使用工具箱配置的 ToolboxManager,以便创建 VelocityToolboxView 中使用的 ChainedContext。

如果路径不存在,此方法会在路径前附加一个“/”,然后使用 ServletContext#getResourceAsStream(String path) 读取它。

考虑到这一点,如果将其设置为完整路径,就会成功。从上下文根开始的路径。

I believe it has to be a full path, eg:

/WEB-INF/classes/com/myapp/resources/toolbox.xml

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文