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

Mah*_*leh 2 spring velocity spring-mvc

问候所有我在发送电子邮件时都使用速度模板,我想根据用户区域设置从属性文件中动态读取文本

xml配置:

<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>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

当试图从属性文件中读取文本时:

<span>#springMessage("hi.message")</span>
Run Code Online (Sandbox Code Playgroud)

它没有读取任何东西,或打印默认值,只是打印:

$springMacroRequestContext.getMessage($code)
Run Code Online (Sandbox Code Playgroud)

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

Bri*_*zel 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>
Run Code Online (Sandbox Code Playgroud)

您可以查看spring文档中的示例.

如果Spring没有自动将$springMacroRequestContext变量注入到模型中,您应该自己设置:

model.put("springMacroRequestContext", new RequestContext(request, response, getServletContext(), model));
Run Code Online (Sandbox Code Playgroud)

这基本上就是他们在AbstractTemplateView类中所做的事情.我想你将无法做到这一点,因为你在这里处理电子邮件,而不是网络请求.但这绝对是你能做些什么才能让它发挥作用的暗示.