Problem with Velocity - ResourceNotFoundException when using with Spring MVC

23,748

Solution 1

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

Solution 2

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.

Solution 3

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);
Share:
23,748
TheJediCowboy
Author by

TheJediCowboy

I like to code...

Updated on July 09, 2022

Comments

  • TheJediCowboy
    TheJediCowboy almost 2 years

    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?

  • TheJediCowboy
    TheJediCowboy over 13 years
    I tried this, but it is still unable to find the .vm file, im wondering where my error could be...
  • TheJediCowboy
    TheJediCowboy over 13 years
    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?
  • duffymo
    duffymo over 13 years
    What path did you give it? Should have been "velocity/*.vm". If it's in WEB-INF/classes, you don't need the prefix.
  • TheJediCowboy
    TheJediCowboy over 13 years
    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.
  • duffymo
    duffymo over 13 years
    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?
  • TheJediCowboy
    TheJediCowboy over 13 years
    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!