无法使用电子邮件模板中的速度使用宏?

问题描述:

问候所有 发送电子邮件 时,我想从属性文件,这取决于用户的语言环境无法使用电子邮件模板中的速度使用宏?

XML配置动态读取文本,我使用Velocity模板:

<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) 

我不知道为什么? ,我错过了什么?,有什么帮助?

宏不能Web应用程序之外使用像电子邮件模板,这样一个解决方案是为messageSource通过它像答案传递给虚拟机文件和读取属性文件在这里:

Is it possible to read static text dynamically from property files in velocity template?

当使用速度引擎发送电子邮件时,您可能必须配置您的引擎tu使用春季运送的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

如果春天没有自动注入$springMacroRequestContext可变进你的模型,你应该把它自己:

model.put("springMacroRequestContext", new RequestContext(request, response, getServletContext(), model)); 

这基本上是他们在AbstractTemplateView类做些什么。我想你不能这样做,因为你在这里处理电子邮件,而不是网络请求。但这绝对是暗示你可以做些什么来实现它。

+0

我添加应用上述配置后,现在出现的值$ springMacroRequestContext.getMessage($ code),你怎么看?我还有另一个问题,是在哪里定义速度使用的属性文件? – 2010-12-01 15:10:00