struts2 ognl StrutsTypeConverter for date util and sql
Par Quentin Ambard le lundi, décembre 13 2010, 23:10 - struts2 - Lien permanent
Using struts2 and ognl, dates are automatically converted from the input to a Date object in your controller (util.Date or sql.Date).
The conversion is done using the the SHORT format for the Locale associated with the current request.
If you want to change this, the fastest way is to override ognl default conversion, using a custom StrutsTypeConverter (here called UtilDateConverter and convertToString.
Note this isn't the best practice :
from Struts2 doc:
{{Type conversion should not be used as a substitute for i18n. It is not recommended to use this feature to print out properly formatted dates. Rather, you should use the i18n features of Struts 2 (and consult the JavaDocs for JDK's MessageFormat object) to see how a properly formatted date should be displayed.
}}
Here is the class for all your util.Date objects.
Just create the class in a com.converter package
package com.converter ; public class UtilDateConverter extends StrutsTypeConverter { public Object convertFromString(Map context, String[] values, Class toClass) { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); try { Date date = (Date) sdf.parse(values[0]); return new java.sql.Date(date.getTime()) ; } catch (ParseException e) { return values[0]; } } public String convertToString(Map context, Object o) { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); return sdf.format(o); } }
Same thing for sql.Date
package com.converter ; public class SqlDateConverter extends UtilDateConverter { public Object convertFromString(Map context, String[] values, Class toClass) { Date date = (Date) super.convertFromString(context, values, toClass); return new java.sql.Date(date.getTime()) ; } }
Now if we want this to be used by all our controller, we need to create the following "xwork-conversion.properties" at the root of the classpath :
java.util.Date=com.converter.UtilDateConverter java.sql.Date=com.converter.SqlDateConverter
That's it.
Conversions are now done using our own class :)
(Note that you can do the same but just for a specific controller)
Commentaires
I am extremely impressed along with your writing skills and also with the format in your weblog. Is this a paid theme or did you customize it your self? Either way stay up the nice high quality writing, it is rare to peer a great blog like this one today..
Good Job. Thanks for doing such a good job. I'll return back to read more and recommend my coworkers about you.
Hi my friend! I want to say that this post is awesome, great written and include almost all significant infos. I would like to look extra posts like this .
There are many famous fashion designers and fashion handbags
http://www.voguesking.us
http://www.handbagshermes.org.uk
http://www.replicacartierhandbags.n...
You actually make it seem so easy along with your presentation however I to find this topic to be actually one thing that I think I might by no means understand. It sort of feels too complicated and extremely broad for me. I am looking forward on your subsequent post, I'll attempt to get the grasp of it!