Spring /WEB-INF子文件夹中的Velocity电子邮件模板

Spring /WEB-INF子文件夹中的Velocity电子邮件模板,spring,velocity,email,Spring,Velocity,Email,我编码了发送电子邮件的服务。现在我必须集成velocity-freamwork,所以我编辑了我的mail-context.xml .... <bean id="mailService" class="xxx.xxxx.xxxx.service.MailServiceImpl"> <property name="mailSender" ref="mailSender" /> <property name="simpleMailMessa

我编码了发送电子邮件的服务。现在我必须集成velocity-freamwork,所以我编辑了我的mail-context.xml

....
<bean id="mailService" class="xxx.xxxx.xxxx.service.MailServiceImpl">
        <property name="mailSender" ref="mailSender" />
        <property name="simpleMailMessage" ref="customeMailMessage" />
        <property name="velocityEngine" ref="velocityEngine" />
    </bean>

    <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/email</prop>
            </props>
        </property>
    </bean>
...
然后,堆栈跟踪被删除

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'test.vm'
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474)
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
    at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:343)
    at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:320)
    at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplate(VelocityEngineUtils.java:58)
    at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplateIntoString(VelocityEngineUtils.java:122)
    at xx.xxxxxxx.xxx.service.MailServiceImpl.sendEmailVelocity(MailServiceImpl.java:410)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy61.sendEmailVelocity(Unknown Source)org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'test.vm'
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474)
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
    at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:343)
    at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:320)
    at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplate(VelocityEngineUtils.java:58)
    at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplateIntoString(VelocityEngineUtils.java:122)
    at xx.xxxxx.xxxx.service.MailServiceImpl.sendEmailVelocity(MailServiceImpl.java:410)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy61.sendEmailVelocity(Unknown Source)

有什么建议吗?

velocityEngine
bean更改为

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="resourceLoaderPath" value="/WEB-INF/email/" />
</bean>


它应该可以工作。

你确定模板
/WEB-INF/email/test.vm
存在吗?当然!我写了这篇文章并把它放到/WEB-INF/email文件夹中。你的资源路径可能会被关闭。您是否配置了其他资源加载程序?我写了一个测试,它按预期工作。唯一的区别是测试使用类路径。这解释了一切:-)出于测试目的,只需将模板存储在类路径上。或者,如果您正在使用maven,请将其放入
test/resources
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="resourceLoaderPath" value="/WEB-INF/email/" />
</bean>