views:

37

answers:

2

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?

+1  A: 

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);
Péter Török
I tried this, but it is still unable to find the .vm file, im wondering where my error could be...
CitadelCSAlum
+4  A: 

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

duffymo
I tried putting my /velocity directory under /WEB-INF/classes/velocity but it still gave me the same problem. Is there a better way to do this or to specify the velocity properties?
CitadelCSAlum
What path did you give it? Should have been "velocity/*.vm". If it's in WEB-INF/classes, you don't need the prefix.
duffymo
Great, this worked, I am curious, is this an alright method of doing this(keeping the velocity templates in the classes directory)? I haven't seen much on the topic, but I have heard from multiple sources the same practice.
CitadelCSAlum
Sure, why would it not be all right? You're simply putting templates in the classpath where the resource loader can find them. What are the potential problems that you're worried about?
duffymo
I am not worried about any problems, I just dont have much experience and have not every put anything in the classes directory, but it works well and I appreciate the help!
CitadelCSAlum