简体   繁体   English

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

[英]Problem with Velocity - ResourceNotFoundException when using with Spring MVC

I am using Spring MVC for my web application and I am integrating Velocity for templating my emails. 我正在将Spring MVC用于我的Web应用程序,并且正在集成Velocity来模板化电子邮件。

I am getting the following 500 error when It attempts to send my email. 尝试发送我的电子邮件时,出现以下500错误。

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. 我知道这意味着什么以及我需要做什么,但是我知道我必须做错了什么,我无法弄清为什么找不到我的.vm文件。

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. 我已经在applicationContext.xml文件中配置了Velocity,如下所示,但我认为可能遗漏了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>

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 我的模板文件的路径是WEB-INF / velocity / templateName.vm

I specify this when using the velocityEngine bean in my controller as well such as the following 我在控制器中使用VelocityEngine bean时指定了此代码,例如以下代码

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? 我需要在build.xml文件中做些什么来确保它能够找到我的模板文件?

I think the problem is that WEB-INF is not part of CLASSPATH. 我认为问题在于WEB-INF不属于CLASSPATH。 You can't expect the ClasspathResourceLoader to find something that isn't in the CLASSPATH. 您不能期望ClasspathResourceLoader找到CLASSPATH中没有的内容。

WEB-INF/classes and all the JARs in WEB-INF/lib are in the CLASSPATH. WEB-INF / classes和WEB-INF / lib中的所有JAR都在CLASSPATH中。 Try moving your folder with the .vm files under WEB-INF/classes and see if that helps. 尝试使用WEB-INF / classes下的.vm文件移动文件夹,看看是否有帮助。

Best idea of all is to follow the Spring docs: 最好的主意是遵循Spring文档:

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

Say you archive *.vm files in a *.jar file. 假设您将* .vm文件归档在* .jar文件中。 And put it in your WEB-INF/lib. 并将其放在您的WEB-INF / lib中。

Then include following snippet in your bean configuration to make it visible to VelocityEngineUtils. 然后在您的bean配置中包含以下代码片段,以使其对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(ie, should be in your class path) between <value>...</value> block. 您可以在<value>...</value>块之间给出每个资源位置(即应该在类路径中)。

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);

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. 如果放入类路径,则开发将陷入困境,因为每次更改速度模板时,servlet容器都会重新加载webapp。

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. 我建议使用org.apache.velocity.tools.view.WebappResourceLoader,它通过不需要将文件放在类路径中而使开发变得更加容易,并且还允许您进行相对包含。

You can also check my post about this: Spring-mvc + Velocity + DCEVM 您还可以检查有关此内容的我的帖子: Spring-mvc + Velocity + DCEVM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM