Hi,
I want to format dates according to the current locale on Velocity pages that are created by a simple controller. Since there is no binding facility, how do I do that?
Thanks,
Vincent
Hi,
I want to format dates according to the current locale on Velocity pages that are created by a simple controller. Since there is no binding facility, how do I do that?
Thanks,
Vincent
Expose a dateTool to your Velocity templates by setting the 'dateToolAttribute' property on either VelocityViewResolver or your individual VelocityView definitions..
Code:<bean id="viewResolver" class="...VelocityViewResolver"> <property name="dateToolAttribute"><value>dateTool</value></property> <!-- other props --> </bean>
then in your VTL do something like;
The date tool that you actually get is a Spring version (not the default Velocity version) which takes account of any Locale resolution strategy you happen to be using.Code:$dateTool.format("dd MMM yyyy HH:mm", $someObject.someDate)
Regards,
Darren Davison.
Public Key: 0xE855B3EA
Thanks for the insight. The problem is that I don't want the locale to be defined by the system, but by the session/browser locale. There are also other things I want to format/parse, for example currencies and custom objects.
I generally require the same functionality CustomEditor. The following code is used in a form. This is the kind of functionality I like to use in my non-form controllers, for displaying lists of results and just plain uneditable information.
Code:/** * Add two custom editors, one to convert a date to the current * locale, and one to convert a gender to a Gender object. This * method is invoked each time a new control object is created. */ protected void initBinder(HttpServletRequest req, ServletRequestDataBinder db) throws Exception { DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT , req.getLocale()); db.registerCustomEditor(Date.class, new CustomDateEditor(df, true)); db.registerCustomEditor(Gender.class, new CustomGenderEditor(true)); }
As mentioned, you can use any locale resolution strategy that Spring supports, and the Velocity tool will take account of this. It will simply work. You can use (for example) an AcceptHeaderLocaleResolver to get locale's specified by the browser.Originally Posted by DaVinci79
Regards,
Darren Davison.
Public Key: 0xE855B3EA