tags:

views:

66

answers:

2

How do you bind a Date to in stripes using a specific format? "MM/dd/yyyy HH:mm:ss"

<s:text name="myDateTime" formatPattern="MM/dd/yyyy HH:mm:ss" />
A: 

SimpleDateFormat will be able to help you.

Jaydeep
I know how to manually format the date with SimpleDateFormat. Stripes supports automatic type conversion from "String" to "Date" but I can't get the right format. I appreciate the response though.
ScArcher2
+2  A: 

Stripes uses Typeconverters for converting request parameters (Strings) to specific types. The javadoc for the default Date type converter can be found here:

net.sourceforge.stripes.validation.DateTypeConverter

To change the default formats it states:

This default set of formats can be changed by providing a different set of format strings in the Stripes resource bundle, or by sub classing and overriding getFormatStrings(). In all cases patterns should be specified using single spaces as separators instead of slashes, dashes or other characters.

And:

The keys used in the resource bundle to specify the format strings and the pre-process pattern are: stripes.dateTypeConverter.formatStrings and stripes.dateTypeConverter.preProcessPattern

If that does not do it for you, you can always roll you're own TypeConverter. This custom type converter can then be bound to a setter in the ActionBean by:

@Validate(converter=YourCustomTypeConverter.class)
public void setDate(Date date) {
  this.date = date;
}

If you want to let the mapping be done automatically you either need to override the default mapper or create another (sub) type. For example, you create your own type converter not for java.util.Date but for your own custom type that inherits from java.util.Date. As it's just a sub type without any extra behavior, the rest of the application can use it as java.util.Date.

Date date;

// No @validate needed, maps to MyCustomDate
public void setDate(MyCustomDate date) {
  this.date = date;
}     
Kdeveloper
Based on the javadoc I was able to use these two StripesResource.properites attributes to make it work. stripes.dateTypeConverter.formatStrings and stripes.dateTypeConverter.preProcessPattern. Thanks!
ScArcher2
Good point, did not know about those properties! I have included them in the answer.
Kdeveloper