javatemplatesvelocitytostringwebwork

Configure velocity to render an object with something other than toString?


Is there a way to configure Velocity to use something other than toString() to convert an object to a string in a template? For example, suppose I'm using a simple date class with a format() method, and I use the same format every time. If all of my velocity code looks like this:

$someDate.format('M-D-yyyy')

is there some configuration I could add that would let me just say

$someDate

instead? (Assuming I'm not in a position to just edit the date class and give it an appropriate toString()).

I'm doing this in the context of a webapp built with WebWork, if that helps.


Solution

  • You could also create your own ReferenceInsertionEventHandler that watches for your dates and automatically does the formatting for you.