2010-12-01 46 views
2

問候所有 發送電子郵件 時,我想從屬性文件,這取決於用戶的語言環境無法使用電子郵件模板中的速度使用宏?

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) 

我不知道爲什麼? ,我錯過了什麼?,有什麼幫助?

回答

9

當使用速度引擎發送電子郵件時,您可能必須配置您的引擎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

相關問題