Velocity 问题 - 与 Spring MVC 一起使用时出现 ResourceNotFoundException

发布于 2024-09-27 22:16:34 字数 1408 浏览 0 评论 0原文

我正在将 Spring MVC 用于我的 Web 应用程序,并集成 Velocity 来为我的电子邮件建立模板。

当它尝试发送我的电子邮件时,我收到以下 500 错误。

org.apache.velocity.exception.ResourceNotFoundException: 
Unable to find resource '/WEB-INF/velocity/registrationEmail.vm'

我知道这意味着什么以及我需要做什么,但我知道我一定做错了什么,而且我不明白为什么它找不到我的 .vm 文件。

我已在 applicationContext.xml 文件中配置了速度,如下所示,但我相信我可能会遗漏 Velocity 需要查找该文件的必要属性。

<bean id="velocityEngine" 
    class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="velocityProperties">
             <value>
              resource.loader=class
               class.resource.loader.class=
               org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
             </value>
        </property>
    </bean>
    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
     <property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
    </bean>

我相信这可能是我需要进行一些更改/添加的地方,但我不确定。

我的模板文件的路径是WEB-INF/velocity/templateName.vm

我在控制器中使用velocityEngine bean时指定了这个路径,如下所示

String text = VelocityEngineUtils.mergeTemplateIntoString(
                       velocityEngine, "/WEB-INF/velocity/registrationEmail.vm", test);

我需要做些什么吗在我的 build.xml 文件中以确保它能够找到我的模板文件?

I am using Spring MVC for my web application and I am integrating Velocity for templating my emails.

I am getting the following 500 error when It attempts to send my email.

org.apache.velocity.exception.ResourceNotFoundException: 
Unable to find resource '/WEB-INF/velocity/registrationEmail.vm'

I am aware of what this means and what I need to do, but I know that I must be doing something wrong and I cant figure out why it cant find my .vm files.

I have configured velocity in my applicationContext.xml file as below, but I believe I might be leaving necessary properties out that Velocity needs to find the file.

<bean id="velocityEngine" 
    class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="velocityProperties">
             <value>
              resource.loader=class
               class.resource.loader.class=
               org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
             </value>
        </property>
    </bean>
    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
     <property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
    </bean>

I believe this might be where I need to make some changes/additions but I am not sure.

The path to my template files is WEB-INF/velocity/templateName.vm

I specify this when using the velocityEngine bean in my controller as well such as the following

String text = VelocityEngineUtils.mergeTemplateIntoString(
                       velocityEngine, "/WEB-INF/velocity/registrationEmail.vm", test);

Is there something I need to do in my build.xml file to make sure that it is able to find my template files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

节枝 2024-10-04 22:16:34

我认为问题在于 WEB-INF 不是 CLASSPATH 的一部分。您不能指望 ClasspathResourceLoader 找到 CLASSPATH 中没有的内容。

WEB-INF/classes 以及 WEB-INF/lib 中的所有 JAR 都位于 CLASSPATH 中。尝试将包含 .vm 文件的文件夹移动到 WEB-INF/classes 下,看看是否有帮助。

最好的想法是遵循 Spring 文档:

http://static.springsource.org/spring/docs/2.5.x/reference/view.html#view-velocity

I think the problem is that WEB-INF is not part of CLASSPATH. You can't expect the ClasspathResourceLoader to find something that isn't in the CLASSPATH.

WEB-INF/classes and all the JARs in WEB-INF/lib are in the CLASSPATH. Try moving your folder with the .vm files under WEB-INF/classes and see if that helps.

Best idea of all is to follow the Spring docs:

http://static.springsource.org/spring/docs/2.5.x/reference/view.html#view-velocity

小苏打饼 2024-10-04 22:16:34

假设您将 *.vm 文件归档到 *.jar 文件中。并将其放入您的 WEB-INF/lib 中。

然后在您的 bean 配置中包含以下代码片段,使其对 VelocityEngineUtils 可见。

像魅力一样工作..!


<属性名称=“resourceLoaderPath”>
<值>类路径:com/test/mail

您可以在 ... 块之间指定每个资源位置(即应该位于您的类路径中)。

Say you archive *.vm files in a *.jar file. And put it in your WEB-INF/lib.

Then include following snippet in your bean configuration to make it visible to VelocityEngineUtils.

Work like a charm..!

<bean class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath">
<value>classpath:com/test/mail</value>
</property>
</bean>

You can give what every your resource location(i.e, should be in your class path) between <value>...</value> block.

踏月而来 2024-10-04 22:16:34

我遇到过类似的问题,根本原因是使用绝对路径。因此,请尝试不使用前导 '/'

String text = VelocityEngineUtils.mergeTemplateIntoString(
        velocityEngine, "WEB-INF/velocity/registrationEmail.vm", test);

I have experienced a similar problem and there the root cause turned out to be the usage of absolute path. So try it without the leading '/':

String text = VelocityEngineUtils.mergeTemplateIntoString(
        velocityEngine, "WEB-INF/velocity/registrationEmail.vm", test);
信愁 2024-10-04 22:16:34

这很痛苦。如果您放入类路径,那么开发就会变得地狱,因为每次您在速度模板中进行更改时,Servlet 容器都会重新加载 Web 应用程序。

我建议使用 org.apache.velocity.tools.view.WebappResourceLoader,它不需要文件位于类路径中,从而使开发变得更加容易,并且还允许您执行相对包含。

您还可以查看我的帖子: Spring-mvc + Velocity + DCEVM

This is painful. If you put in the classpath then the development becomes hell, since the servlet container would reload the webapp every time you make a change in the velocity templates.

I would recommend using the org.apache.velocity.tools.view.WebappResourceLoader, which makes development much easier by not requiring the files to be in the classpath and also allows you to do relative includes.

You can also check my post about this: Spring-mvc + Velocity + DCEVM

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