开发者

Problem with Velocity - ResourceNotFoundException when using with Spring MVC

开发者 https://www.devze.com 2023-01-20 14:50 出处:网络
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.

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.VelocityEngineFa开发者_如何学编程ctoryBean">
        <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?


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


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.


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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消