Sunday, January 25, 2009

Struts 2/dojo datepicker tag sucks with non US locale

I have been messing around with Struts 2 datepicker tag. The goal is to submit date and time for a date field.

You make a Date field in Struts 2 action object. And you put in your JSP the datepicker tag like this:

<s:datetimepicker name="date_planned_date" label="" displayformat="dd.MM.yyyy" type="date" value="%{date_planned_date}" />
Now it is important not to get the locales mixed up. So I have been forcing the locale on EVERY form like that:

<s:hidden name="request_locale" value="et_EE" />
And just in case I have my locale also specified in struts.xml:

<constant name="struts.locale" value="et_EE" />
Anyway this setup doesn't work, because datepicker is not able to parse the date in the "value" param. So we omit the value param and things start working. But in our case we NEEDED the value param, because we also wanted to submit time but we didn't want to use the built-in TIME picker, but instead a text-field.

So the solution for us was to make two separate String fields in our Struts 2 application to wich we submited the date and time part of the datetime. And upon saving to DB we constructed the date out of those string fields.

With s:datepicker any "out of the box" setup for i18n does not work and as always with Struts customizations need a lot of extra coding. The datepicker tag should just work with ANY date it is given with the "value" param. Currently it does not - Java's hard work and no fun!

No comments:

Post a Comment