views:

76

answers:

2

Default date format in Spring WebFlow is "yyyy-MM-dd".

How to change to another format? "dd.mm.yyyy" for example. Thx.

A: 

I think like this:

StrginToDate std = new StringToDate();
std.setPattern("dd.MM.yyyy);
cuh
+1  A: 

Sorry for the late post but here is what you have to do. Spring Webflow does custom Data Binding. Its similar to how Spring MVC does it. The difference though is where it handles it. Spring MVC handles it on the controller level ( using the @InitBinder ).

Spring webflow does it on the binding level. Before executing a transition webflow will bind all parameter values to the object then validates the form (if validate="true") then invokes the transition on a successful validation.

What you need to do is to get webflow to change the means in which it binds a Date. You can do this by writing a custom converter.

First you will need a conversion service:

@Component("myConversionService")
public class MyConversionService extends  DefaultConversionService {

    public void MyConversionService() {

    }
}

Webflow will use this service to determine what special binding needs to be accounted for. Now just write your specific date binder (keep in mind webflow defaults a date binder you will just override it).

@Component
public class MyDateToString extends StringToObject {

    @Autowired
    public MyDateToString(MyConversionService conversionService) {
        super(Date.class);
        conversionService.addConverter(this);
    }

    @Override
    protected Object toObject(String string, Class targetClass) throws Exception {
        try{
            return new SimpleDateFormat("MM\dd\yyyy").parse(string);//whatever format you want
        }catch(ParseException ex){
            throw new ConversionExecutionException(string, String.class, targetClass, Date.class);//invokes the typeMissmatch
        }       
    }
}
John V.