Search code examples
datedata-bindingbindingconvertersspring-webflow

Spring webflow date binder


I'm quite new working with webflow, and I having some troubles to make a custom converter for date binding (I need to changue the default pattern for 'dd-MM-yyyy')

So I'm trying to do something like this:

    <view-state id="viewAnexos" view="myview" model="myModelBean">  
        <binder> 
            <binding property="anyDateOfTheBean" required="true" converter="customConverter"/>     <!-- the type is java.util.Date -->
        </binder>

        <transition on="saveAnexo" to="viewAnexos" bind="true">
            <evaluate expression="myController.saveAction(myModelBean, messageContext)" />
        </transition>
    </view-state>

And I've defined the ConversionService

    @Service("conversionService")
    public class FlowConversionService extends DefaultConversionService {

        public void FlowConversionService() {
            addDefaultConverters();
        }

        @Override
        protected void addDefaultConverters() {
            super.addDefaultConverters();
            addConverter(new StringToDateCustomConverter());
            addConverter("customCoverter,"new StringToDateCustomConverter()); //This method is deprecated, so how should I do it?
        }
    }

And the CustomConverter

public class StringToDateCustomConverter extends StringToObject {

private DateFormat format = null;

public StringToDateCustomConverter() {
    super(StringToDateCustomConverter.class);
    format = new SimpleDateFormat("dd/MM/yyyy");
}

@Override
protected Object toObject(String string, Class targetClass) throws ParseException {
    return format.parse(string);
}

@Override
protected String toString(Object object) {
    return format.format((Date) object);
}

}

And in my servlet.xml file, I've defined

    <webflow:flow-builder-services 
    id="flowBuilderServices" 
    view-factory-creator="mvcViewFactoryCreator"
    conversion-service="conversionService"/>

    <bean id="conversionService" class="es.xunta.emprego.converter.FlowConversionService"/> 

And after all this I'm having the next error:

        org.springframework.binding.convert.ConversionExecutorNotFoundException: Custom ConversionExecutor with id 'customConverter' cannot convert from sourceClass [java.lang.String] to targetClass [java.util.Date]

Any ideas of what am I missing here..? Thanks in advance...


Solution

  • I've just changued a few things, and it works now.

    1.-Renamed the StringToDateCustomConverter to StringToDate.

    2.-The ConversionService has changued this way

    @Service("conversionService")
    public class FlowConversionService extends DefaultConversionService {
    
        @Override
        protected void addDefaultConverters() {
            super.addDefaultConverters();
            addConverter(new StringToDate()); //This is overrided
        }
    
    }
    

    3.-The binder in the flow.xml is not necessary anymore, so...

        <view-state id="viewAnexos" view="myview" model="myModelBean"> 
            <transition on="saveAnexo" to="viewAnexos" bind="true">
                <evaluate expression="myController.saveAction(myModelBean, messageContext)" />
            </transition>
        </view-state>
    

    And that's it, it's working fine!!