在Spring中使用Velocity的WebappResourceLoader

前端之家收集整理的这篇文章主要介绍了在Spring中使用Velocity的WebappResourceLoader 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试使用Velocity创建由SpringJavaMailSender类发送的电子邮件模板.我决定用来在Web应用程序中找到Velocity模板的资源加载器是WebappResourceLoader,它位于Velocity工具jar中.

但是,根据我使用WebappResourceLoader的方式,我会在启动Web应用程序时获得NPE或找不到模板.

如果在Spring应用程序上下文中为Velocity引擎指定了初始化属性,则将获得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.任何帮助,不胜感激.谢谢!

最佳答案
您需要传递ServletContext对象:

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

另一种选择是改为使用ClassPathResourceLoader并将模板放在classpath中.

原文链接:https://www.f2er.com/spring/531667.html

猜你在找的Spring相关文章