2010-06-15 35 views
5

我一直在試圖按照this example和使用the reference來指導我,但我沒有運氣。如何在spring 3/webflow 2中註冊自定義轉換服務?

我已經定義了一個轉換器:

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 

回答

9

Spring MVC和春天的Webflow使用不同的類型轉換器層次結構。因此, <mvc:annotation-driven ...>要求org.springframework.core.convert.ConversionService,但<webflow:flow-builder-services ...>要求org.springframework.binding.convert.ConversionService

+2

謝謝!這解決了問題。我只是刪除了「」位,因爲我主要關注webflow中的表單綁定,而在常規的MVC世界中則更少。現在,原始代碼無誤地工作。 – nont 2010-06-15 16:35:15

0

在上面的代碼中,你應該寫super(Date.class)而不是super(StringToDateTwoWayConverter.class)

相關問題