Announcement Announcement Module
Collapse
No announcement yet.
How to configure Velocity Tools for a VelocityEngine? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to configure Velocity Tools for a VelocityEngine?

    Hi,

    It may be a simple question, but now I'm dealing with velocity-based emails. I've done the tutorial as Matt Raible discussed.

    I'd like to use the $format.number() velocity tool (results as JSTL <format:number/> tag does). How can I configure the VelocityEngine to use the toolbox.xml?

    Thanks for any help.

    Thomas

  • #2
    You can configure DateTool and NumberTool attribute as follows:
    Code:
      <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="numberToolAttribute"><value>numberTool</value></property>
        <property name="dateToolAttribute"><value>dateTool</value></property>
        <property name="suffix"><value>.vm</value></property>
      </bean>
    and in you velocity templates you use:
    Code:
    $dateTool.format&#40;&#41;
    $numberTool.format&#40;&#41;
    HTH

    Comment


    • #3
      Thanks Omar. One more question? Can you help me on how I could set only the VelocityEngine to use the numberTool. I have only a VelocityEngine in my mail-velocity-merging manager, so I'm not having any view resolver - cannot have here a VelocityViewResolver.

      UPDATE 1
      Finally solved. I found that velocity tools are added simply into the velocity context, that is based on the map model passed to velocityEngine.mergeTemplate() method.

      Thomas

      Comment


      • #4
        Could somebody who has done this post an example, please?

        If I have:

        Code:
        result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, model);
        where model is a Map consisting of parameter names and their values that will be passed to Velocity, how do I specify tools? What would their names and values be that I place in the map?

        Thanks.

        Comment


        • #5
          Okay I think I figured it out. I just create a tool object, either via spring config or programatically, add it to my model Map like:

          Code:
          map.put("toolName", tool);
          And then access the tool using toolName in the template like any other parameter.

          I'll be trying it at work tomorrow, but feel free to correct me if I'm wrong.

          Comment


          • #6
            try this one, then u can use your code

            Originally posted by jbodnar View Post
            Could somebody who has done this post an example, please?

            If I have:

            Code:
            result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, model);
            where model is a Map consisting of parameter names and their values that will be passed to Velocity, how do I specify tools? What would their names and values be that I place in the map?

            Thanks.

            <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEng ineFactoryBean">
            <property name="velocityProperties">
            <value>
            resource.loader=class
            class.resource.loader.class=org.apache.velocity.ru ntime.resource.loader.ClasspathResourceLoader
            </value>
            </property>
            </bean>

            Comment

            Working...
            X