我一直在尝试关注this example并使用the reference来指导我,但我没有运气。
我已经定义了一个转换器:
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);
}
}
和conversionService:
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" .../>
(显式实例化显示相同的错误)
然而,在启动时,我受到了这个例外的欢迎:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.core.convert.ConversionService]: Could not convert constructor argument value of type [com.yadayada.converter.ApplicationConversionService] to required type [org.springframework.core.convert.ConversionService]: Failed to convert value of type 'com.yadayada.converter.ApplicationConversionService' to required type 'org.springframework.core.convert.ConversionService'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.yadayada.converter.ApplicationConversionService] to required type [org.springframework.core.convert.ConversionService]: no matching editors or conversion strategy found
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:687)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:195)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:993)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:897)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:270)
... 60 more
我很困惑为什么它不起作用。转换服务通过其基类实现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
答案 0 :(得分:9)
Spring MVC和Spring Webflow使用不同的类型转换器层次结构。所以,
<mvc:annotation-driven ...>
需要org.springframework.core.convert.ConversionService
,但<webflow:flow-builder-services ...>
需要org.springframework.binding.convert.ConversionService
。
答案 1 :(得分:0)
在上面的代码中,您应该写super(Date.class)
而不是super(StringToDateTwoWayConverter.class)
。