Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change default format in StringToDate? Spring WebFlow

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

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

like image 599
Alexander Avatar asked Jul 22 '10 08:07

Alexander


1 Answers

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
        }       
    }
}
like image 137
John Vint Avatar answered Sep 19 '22 01:09

John Vint