Java 如何在Spring3/WebFlow2中注册自定义转换服务?
我一直试图跟随并用它来指引我,但我没有运气 我定义了一个转换器:Java 如何在Spring3/WebFlow2中注册自定义转换服务?,java,spring,spring-webflow,Java,Spring,Spring Webflow,我一直试图跟随并用它来指引我,但我没有运气 我定义了一个转换器: import org.springframework.binding.convert.converters.StringToObject; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Date; public class StringToDate
import org.springframework.binding.convert.converters.StringToObject;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
public class StringToDateTwoWayConverter extends StringToObject {
private DateFormat format = null;
public StringToDateTwoWayConverter () {
super(StringToDateTwoWayConverter.class);
format = new SimpleDateFormat("MM/dd/yyyy");
}
@Override
protected Object toObject(String string, Class targetClass) throws Exception {
Date date = null;
try {
date = format.parse(string);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
return date;
}
@Override
protected String toString(Object object) throws Exception {
Date date = (Date) object;
return format.format(date);
}
}
和转换服务:
import org.springframework.binding.convert.service.DefaultConversionService;
import org.springframework.stereotype.Component;
@Component("conversionService")
public class ApplicationConversionService extends DefaultConversionService
{
@Override
protected void addDefaultConverters() {
super.addDefaultConverters();
this.addConverter(new StringToDateTwoWayConverter());
this.addConverter("shortDate", new StringToDateTwoWayConverter());
}
}
并将其配置为:
<mvc:annotation-driven conversion-service="conversionService" />
<webflow:flow-builder-services id="flowBuilderServices" conversion-service="conversionService" .../>
我完全搞不懂它为什么不起作用。转换服务通过其基类实现ConversionService,因此我看不出问题所在。非常感谢您的任何见解
为了响应下面的回答,我尝试更改服务以实现其他转换服务:
import org.springframework.stereotype.Component;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.FormattingConversionService;
@Component ("conversionService")
public class ApplicationConversionService extends FormattingConversionService implements org.springframework.core.convert.ConversionService
{
public ApplicationConversionService() {
this.addConverter(new StringToDateConverter2());
}
}
但现在我以另一种方式失败了:
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.yadayada.converter.ApplicationConversionService] to required type [org.springframework.binding.convert.ConversionService] for property 'conversionService': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:291)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:155)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:461)
... 86 more
SpringMVC和SpringWebFlow使用不同层次的类型转换器。所以
需要org.springframework.core.convert.ConversionService
,但是
需要org.springframework.binding.convert.ConversionService
在上面的代码中,你应该写超级(Date.class)
而不是超级(StringToDateTwoWayConverter.class)
谢谢!这就解决了问题。我刚刚删除了“”位,因为我最关心的是webflow中的表单绑定,而在常规MVC世界中则更少。现在,原始代码可以正常工作。
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.yadayada.converter.ApplicationConversionService] to required type [org.springframework.binding.convert.ConversionService] for property 'conversionService': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:291)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:155)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:461)
... 86 more