PDA

View Full Version : How to configure Velocity Tools for a VelocityEngine?


tsandor
Nov 23rd, 2004, 10:18 AM
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

irbouho
Nov 23rd, 2004, 10:12 PM
You can configure DateTool and NumberTool attribute as follows:
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.Velo cityViewResolver">
<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:
$dateTool.format()
$numberTool.format()
HTH

tsandor
Nov 24th, 2004, 04:19 AM
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

jbodnar
Jun 13th, 2007, 07:02 PM
Could somebody who has done this post an example, please?

If I have:

result = VelocityEngineUtils.mergeTemplateIntoString(veloci tyEngine, 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.

jbodnar
Jun 13th, 2007, 10:03 PM
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:

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.