4

我是使用 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]

关于我在这里缺少什么的任何想法..?提前致谢...

4

1 回答 1

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中的binder不再需要了,所以...

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

就是这样,它工作正常!

于 2013-08-23T08:56:40.067 回答