Unable to use macros with velocity in email templates?

greetings all i am using velocity templates when sending emails and i want to read texts dynamically from property files depending on user locale

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

and when trying to read the text from property file like :

<span>#springMessage("hi.message")</span>

it doesn't read any thing, or prints the default value, just prints:

$springMacroRequestContext.getMessage($code)

i don't know why? , am i missing something ?, any help ?

Answers


When using the velocity engine for sending emails, you may have to configure your engine tu use the velocimacro librabry shipped within spring.

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

You can check the example in spring documentation.

If Spring doesn't inject automatically the $springMacroRequestContext variable into your model, you should put it yourself:

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

That's basically what they do in the AbstractTemplateView class. I guess you won't be able to do it, since you're handling emails here, and not web requests. But that's definitely a hint on what you can do to get it working.


Need Your Help

How do I recover a semaphore when the process that decremented it to zero crashes?

linux gcc g++ semaphore

I have multiple apps compiled with g++, running in Ubuntu. I'm using named semaphores to co-ordinate between different processes.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.