Java 速度问题 - ResourceNotFoundException 与 Spring MVC 一起使用时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3949589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem with Velocity - ResourceNotFoundException when using with Spring MVC
提问by TheJediCowboy
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 .vmfiles.
我知道这意味着什么以及我需要做什么,但我知道我一定做错了什么,我无法弄清楚为什么它找不到我的.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 查找文件所需的必要属性。
<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
我在我的控制器中使用速度引擎 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 文件中做些什么来确保它能够找到我的模板文件吗?
采纳答案by duffymo
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 不是 CLASSPATH 的一部分。您不能期望ClasspathResourceLoader
找到不在 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.
WEB-INF/classes 和 WEB-INF/lib 中的所有 JAR 都在 CLASSPATH 中。尝试使用 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
回答by Péter T?r?k
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);
回答by Rafael Sanches
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
回答by mahesh nanayakkara
Say you archive *.vm files in a *.jar file. And put it in your WEB-INF/lib.
假设您将 *.vm 文件存档在 *.jar 文件中。并将其放在您的 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>
<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.
您可以在<value>...</value>
块之间提供每个资源位置(即,应该在您的类路径中)。