Spring webflow日期活页夹

时间:2013-08-22 12:39:04

标签: date data-binding binding converter spring-webflow

我是一个非常新的使用webflow的人,我有一些麻烦来制作一个自定义日期绑定转换器(我需要改变'dd-MM-yyyy'的默认模式)

所以我想尝试这样的事情:

    <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>

我已经定义了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?
        }
    }

和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);
}

}

在我的servlet.xml文件中,我已经定义了

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

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

毕竟我遇到了下一个错误:

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

我在这里失踪的任何想法......?提前谢谢......

1 个答案:

答案 0 :(得分:1)

我只是做了几件事,现在就可以了。

1.将StringToDateCustomConverter重命名为StringToDate。

2.- ConversionService以这种方式发展

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

    @Override
    protected void addDefaultConverters() {
        super.addDefaultConverters();
        addConverter(new StringToDate()); //This is overrided
    }

}

3.-不再需要flow.xml中的绑定器,所以......

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

就是这样,它工作得很好!!