6

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

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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Alexander
  • 781
  • 2
  • 9
  • 21

3 Answers3

4

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 Vint
  • 39,695
  • 7
  • 78
  • 108
  • i'm trying to do a conversion service for a many-to-many relationship. looking at your answer, have i to add BOTH classes ("MyConversionService" and "MyDateToString") or MyDateToString is a specific implementation of MyConversionService? then, what do i have to put in MyConversionService ?thanks – WoutVanAertTheBest Jul 09 '14 at 13:03
  • @lethal.industry You would need to create the `MyConversionService` and add your `MyDateToString` to that conversion service. So you would need to create both. MyDateString does not implement ConversionService – John Vint Jul 09 '14 at 13:48
  • hmm sorry but for me it's not clear. can you please provide me an example? I need to create a String to @OneToMany Set converter – WoutVanAertTheBest Jul 10 '14 at 09:43
  • Oh. So you will convert a String to a set of your objects. If you really want an example, you should open up a new question as I don't want this one (written 4 years ago) to be updated. But your StringToObject method should return a type of `MyEntity`. Spring MVC and webflow handles collections for you if they are passed in correctly (which web forms take care of for your). What you need to do is have your `toObject` method return the set. – John Vint Jul 10 '14 at 13:20
  • If you do ask another question feel free to link it here. – John Vint Jul 10 '14 at 13:21
  • thank you very much @JohnVint. i posted this question some days ago :http://stackoverflow.com/questions/24608519/string-to-type-onetomany-converter-spring-hibernate . For me is a big surprise reading that's a very simple thing to do as i'm stuck on this from many days. thank you – WoutVanAertTheBest Jul 10 '14 at 13:50
2

I achieved that by creating this bean:

@Component(value = "applicationConversionService") public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {

@Override
protected void installFormatters(FormatterRegistry registry) {
  // Register the default date formatter provided by Spring
  registry.addFormatter(new DateFormatter("dd/MM/yyyy"));
} }

Registered the bean like this:

<bean id="defaultConversionService" class="org.springframework.binding.convert.service.DefaultConversionService">
    <constructor-arg ref="applicationConversionService" />
</bean>

Then referenced by:

<mvc:annotation-driven conversion-service="applicationConversionService" />

And:

<!-- Enables custom conversion, validation and sets a custom vew factory creator on Spring Webflow -->
<webflow:flow-builder-services id="flowBuilderServices" conversion-service="defaultConversionService" validator="validator" view-factory-creator="mvcViewFactoryCreator" />

With this configuration it works fine for me. I hope it helps.

szegedi
  • 863
  • 8
  • 20
  • installFormatters appears to no longer exist in FormattingConversionServiceFactoryBean anymore. – Luke May 27 '16 at 01:57
-1

I think it must be like this:

StringToDate std = new StringToDate();
std.setPattern("dd.MM.yyyy");
cuh
  • 3,723
  • 4
  • 30
  • 47