Velocity 邮件模板在Spring 中发邮件报unable to find resource 'WEB-INF/test.vm' in any resource loader导常。配置如下:
<bean id="templateMail" class="com.chenlb.mail.VelocityTemplateMessage">
<property name="javaMailSender" ref="mailSender"></property>
<property name="from" value="${mail.from}"></property>
<property name="encoding" value="UTF-8"></property>
<property name="templateLocation" value="WEB-INF/test.vm"></property>
<property name="velocityEngine">
<bean class="org.springframework.ui.velocity.VelocityEngineFactoryBean"></bean>
</property>
<property name="title" value="www.blogjava.net/chenlb"></property>
</bean>
邮件内容生成如下:
VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateLocation, encoding, model);
但导常,说不可用的资源。
说明:VelocityEngineUtils.mergeTemplateIntoString()里的templateLocation的文件,不会像Spring一样找。然后就看Spring 的 VelocityEnginFactory API和试,最后发现<bean class="org.springframework.ui.velocity.VelocityEngineFactoryBean"></bean>
单独定义,并配置resourceLoaderPath属性即可搞定。
配置后的:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="WEB-INF/"></property>
</bean>
<bean id="templateMail" class="com.chenlb.mail.VelocityTemplateMessage">
<property name="javaMailSender" ref="mailSender"></property>
<property name="from" value="${mail.from}"></property>
<property name="encoding" value="UTF-8"></property>
<property name="templateLocation" value="test.vm"></property>
<property name="velocityEngine" ref="velocityEngine"></property>
<property name="title" value="wwww.blogjava.net/chenlb"></property>
</bean>
resourceLoaderPath是由Spring设置过的,跟平常的(Spring)文件资源一样方式引用,Spring真的太方便了。
<bean id="templateMail" class="com.chenlb.mail.VelocityTemplateMessage">
<property name="javaMailSender" ref="mailSender"></property>
<property name="from" value="${mail.from}"></property>
<property name="encoding" value="UTF-8"></property>
<property name="templateLocation" value="WEB-INF/test.vm"></property>
<property name="velocityEngine">
<bean class="org.springframework.ui.velocity.VelocityEngineFactoryBean"></bean>
</property>
<property name="title" value="www.blogjava.net/chenlb"></property>
</bean>
邮件内容生成如下:
VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateLocation, encoding, model);
但导常,说不可用的资源。
说明:VelocityEngineUtils.mergeTemplateIntoString()里的templateLocation的文件,不会像Spring一样找。然后就看Spring 的 VelocityEnginFactory API和试,最后发现<bean class="org.springframework.ui.velocity.VelocityEngineFactoryBean"></bean>
单独定义,并配置resourceLoaderPath属性即可搞定。
配置后的:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="WEB-INF/"></property>
</bean>
<bean id="templateMail" class="com.chenlb.mail.VelocityTemplateMessage">
<property name="javaMailSender" ref="mailSender"></property>
<property name="from" value="${mail.from}"></property>
<property name="encoding" value="UTF-8"></property>
<property name="templateLocation" value="test.vm"></property>
<property name="velocityEngine" ref="velocityEngine"></property>
<property name="title" value="wwww.blogjava.net/chenlb"></property>
</bean>
resourceLoaderPath是由Spring设置过的,跟平常的(Spring)文件资源一样方式引用,Spring真的太方便了。