views:

38

answers:

1

I'm trying to use Velocity to create an email template that is mailed by Spring's JavaMailSender class. The resource loader that I decided to use to find the Velocity template in my web app is WebappResourceLoader which is located in the Velocity tool jar.

However, depending on how I use the WebappResourceLoader, I get either a NPE when the web app starts up or the template can't be found.

If I specify the init properties for the Velocity engine in my Spring application context I get the NPE. My configuration is as follows:

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
  <property name="velocityProperties">
        <props>
        <prop key="resource.loader">webapp</prop>
        <prop key="webapp.resource.loader.class">org.apache.velocity.tools.view.WebappResourceLoader</prop>
        <prop key="webapp.resource.loader.path">/WEB-INF/velocity/</prop>
        </props>
   </property>
</bean>

The stack trace I get when the app is starting up is:

java.lang.NullPointerException
at org.apache.velocity.tools.view.WebappResourceLoader.getResourceStream(WebappResourceLoader.java:145)
at org.apache.velocity.runtime.resource.loader.ResourceLoader.resourceExists(ResourceLoader.java:224)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getLoaderForResource(ResourceManagerImpl.java:641)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getLoaderNameForResource(ResourceManagerImpl.java:624)
at org.apache.velocity.runtime.RuntimeInstance.getLoaderNameForResource(RuntimeInstance.java:1464)
at org.apache.velocity.runtime.VelocimacroFactory.initVelocimacro(VelocimacroFactory.java:159)
at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:261)
at org.apache.velocity.app.VelocityEngine.init(VelocityEngine.java:107)
at org.springframework.ui.velocity.VelocityEngineFactory.createVelocityEngine(VelocityEngineFactory.java:251)
at org.springframework.ui.velocity.VelocityEngineFactoryBean.afterPropertiesSet(VelocityEngineFactoryBean.java:57)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1460)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1398)

To fix this I changed my application context to as below:

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean" />

And then in the service class, right before the code to merge application data with the template (which is at /WEB-INF/velocity and named regemail.vm) I added the following code:

    velocityEngine.addProperty("resource.loader", "webapp");
    velocityEngine.addProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.WebappResourceLoader");
    velocityEngine.addProperty("webapp.resource.loader.path", "/WEB-INF/velocity/");
    velocityEngine.setApplicationAttribute("javax.servlet.ServletContext", "localhost:8080");

The app starts fine but when the email is going to be sent, I get the following error:

SEVERE: Servlet.service() for servlet default threw exception
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'regmail.vm'
at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:483)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:354)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1400)
at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:370)
at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:345)
at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplate(VelocityEngineUtils.java:58)
at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplateIntoString(VelocityEngineUtils.java:122)
at com.mywebapp.web.service.RegistrationServiceImpl$1.prepare(RegistrationServiceImpl.java:60)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:353)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
at com.mywebapp.web.service.RegistrationServiceImpl.sendRegEmail(RegistrationServiceImpl.java:65)
at com.mywebapp.web.controller.SignUpController.onSubmit(SignUpController.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

I'm using Velocity version 1.6.4 and Velocity tools 2.0. Any help much appreciated. Thanks!

A: 

You need to pass ServletContext object:

velocityEngine.setApplicationAttribute("javax.servlet.ServletContext", request.getSession().getServletContext());

Another option would be using ClasspathResourceLoader instead and putting your templates within classpath.

serg
Thanks serg555. According to the Velocity documentation, the ClasspathResouceLoader should be avoid when used from within a web app. Thanks for the answer though.
richever
So did passing ServletContext worked out?
serg