xml config:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
<value>classpath:messages_ar</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</prop>
<prop key="velocimacro.library">org/springframework/web/servlet/view/velocity/spring.vm</prop>
</props>
</property>
</bean>
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/classes/com/spacerdv/mailTemplates"/>
</bean>
<!--
View resolvers can also be configured with ResourceBundles or XML files. If you need
different view resolving based on Locale, you have to use the resource bundle resolver.
-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".vm"/>
<!-- if you want to use the Spring Velocity macros, set this property to true -->
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
当尝试从属性文件中读取文本时:
<span>#springMessage("hi.message")</span>
它不会读取任何内容,或打印默认值,只需打印:
$springMacroRequestContext.getMessage($code)
我不知道为什么?我错过了什么吗?,有什么帮助吗?
答案 0 :(得分:9)
使用速度引擎发送电子邮件时,您可能需要使用spring中发运的velocimacro librabry配置您的引擎。
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</prop>
<prop key="velocimacro.library">org/springframework/web/servlet/view/velocity/spring.vm</prop>
</props>
</property>
</bean>
您可以查看the example in spring documentation。
如果Spring没有自动将$springMacroRequestContext
变量注入模型,你应该自己设置:
model.put("springMacroRequestContext", new RequestContext(request, response, getServletContext(), model));
这基本上就是他们在AbstractTemplateView班级所做的事情。我想你将无法做到这一点,因为你在这里处理电子邮件,而不是网络请求。但这绝对是你能做些什么才能让它发挥作用的一个暗示。
答案 1 :(得分:0)
宏不能像在电子邮件模板中那样在Web应用之外使用,因此解决方案是将messageSource传递给vm文件并通过它读取属性文件,如下面的答案:
Is it possible to read static text dynamically from property files in velocity template?