Categories
Java Web Development

Using Velocity date tool with Spring MVC

I was having trouble getting Velocity‘s date tool to work with Spring MVC. I followed the instructions, most copied verbatim from what was most likely one ancient source. Those instructions said that to enable Velocity’s date formatting tool, DateTool, I needed to configure the VelocityViewResolver like this (WRONG):

<bean id="viewResolver">
  class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"></bean>
<br /><property name="numberToolAttribute"><value>numbertool</value></property>
<br /><property name="dateToolAttribute"><value>datetool</value></property>
&nbsp;

And then use it as:

$date.format("yyyy-MM-dd", $currentDate)

This would have been cool had I seen it in just one source but more than one source had it. The problem is small but crucial:

The value you give the dateToolAttribute property will be the name of the object Velocity will try to access when you are using the tool. So in the above configuration, the value for dateToolAttribute is “dateTool”, and hence the usage should be:

$dateTool.format(“yyyy-MM-dd”, $currentDate)

It may sound petty but it took me time to discover… lame.

Share
Share