我正在尝试使用Velocity创建一个由Spring的JavaMailSender类邮寄的电子邮件模板。我决定用来在我的Web应用程序中查找Velocity模板的资源加载器是WebappResourceLoader,它位于Velocity工具jar中。
但是,根据我使用WebappResourceLoader的方式,当Web应用程序启动或无法找到模板时,我会获得NPE。
如果我在Spring应用程序上下文中指定Velocity引擎的init属性,我会得到NPE。我的配置如下:
<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>
我在应用启动时获得的堆栈跟踪是:
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)
要解决此问题,我将应用程序上下文更改为:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean" />
然后在服务类中,在将应用程序数据与模板(位于/ WEB-INF / velocity并命名为regemail.vm)合并的代码之前,我添加了以下代码:
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");
应用程序启动正常,但是当要发送电子邮件时,我收到以下错误:
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)
我正在使用Velocity版本1.6.4和Velocity工具2.0。任何帮助非常感谢。谢谢!
答案 0 :(得分:1)
您需要传递ServletContext对象:
velocityEngine.setApplicationAttribute("javax.servlet.ServletContext", request.getSession().getServletContext());
另一种选择是使用ClasspathResourceLoader
代替并将模板放在classpath中。
答案 1 :(得分:1)
如果查看VelocityEngineFactoryBean的文档,您会注意到可以设置'resourceLoaderPath'属性。鉴于您的配置,看起来您已将Velocity模板放在/WEB-INF/velocity/
下,因此将其用作'resourceLoaderPath'的值,而工厂bean应该可以正常加载模板。
答案 2 :(得分:1)
三年后,这是另一个解决方案,它不需要调整你的Java代码来将ServletContext传递给Velocity Engine。
只需在Spring Context文件中使用 VelocityConfigurer 代替 VelocityEngineFactoryBean ,如下所示:
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="velocityPropertiesMap">
<map>
<entry key="runtime.log.invalid.reference"><value>true</value></entry>
<entry key="runtime.log.logsystem.class"><value>org.apache.velocity.runtime.log.Log4JLogChute</value></entry>
<entry key="runtime.log.logsystem.log4j.logger"><value>velocity</value></entry>
<entry key="input.encoding"><value>UTF-8</value></entry>
<entry key="output.encoding"><value>UTF-8</value></entry>
<entry key="directive.include.output.errormsg.start"><value></value></entry>
<entry key="directive.parse.max.depth"><value>10</value></entry>
<entry key="directive.set.null.allowed"><value>true</value></entry>
<entry key="velocimacro.library.autoreload"><value>true</value></entry>
<entry key="velocimacro.permissions.allow.inline"><value>true</value></entry>
<entry key="velocimacro.permissions.allow.inline.to.replace.global"><value>false</value></entry>
<entry key="velocimacro.permissions.allow.inline.local.scope"><value>false</value></entry>
<entry key="velocimacro.context.localscope"><value>false</value></entry>
<entry key="runtime.interpolate.string.literals"><value>true</value></entry>
<entry key="resource.manager.class"><value>org.apache.velocity.runtime.resource.ResourceManagerImpl</value></entry>
<entry key="resource.manager.cache.class"><value>org.apache.velocity.runtime.resource.ResourceCacheImpl</value></entry>
<entry key="resource.loader"><value>webapp, class, ds</value></entry>
<entry key="class.resource.loader.description"><value>Velocity Classpath Resource Loader</value></entry>
<entry key="class.resource.loader.class"><value>org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</value></entry>
<entry key="webapp.resource.loader.class"><value>org.apache.velocity.tools.view.WebappResourceLoader</value></entry>
<entry key="webapp.resource.loader.path"><value>/WEB-INF/views/</value></entry>
<entry key="webapp.resource.loader.cache"><value>false</value></entry>
<entry key="webapp.resource.loader.modificationCheckInterval"><value>2</value></entry>
<entry key="ds.resource.loader.instance"><ref bean="templateLoader"/></entry>
<entry key="ds.resource.loader.resource.table"><value>templates</value></entry>
<entry key="ds.resource.loader.resource.keycolumn"><value>code</value></entry>
<entry key="ds.resource.loader.resource.templatecolumn"><value>content</value></entry>
<entry key="ds.resource.loader.resource.timestampcolumn"><value>updated</value></entry>
<entry key="ds.resource.loader.cache"><value>false</value></entry>
</map>
</property>
</bean>
<bean id="templateLoader"
class="org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader">
<property name="dataSource" ref="yourDataSource"></property>
</bean>