Spring的转换服务没有将转换器调用到String(用于显示目的)

时间:2012-05-21 15:20:50

标签: java spring spring-webflow-2

我正在使用Spring webflow。

我配置了FormattingConversionService。在此转换服务中,我配置了以下内容:

  • ConverterFactory将字符串值转换为MyInterface个实例(绑定到对象)
  • ConverterMyInterface的对象转换为字符串(用于显示)

调用'ConverterFactory`并完美运行。

我的问题是没有调用ConvertertoString()显示在页面上。

如何让Spring将MyInterface的对象实例转换为String以用于显示目的?

这是我的conversionService声明:

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="myclasses.StringToMyInterfaceConverterFactory"/>
            <bean class="myclasses.MyInterfaceToStringConverter"/>
        </set>
    </property>
</bean>

MyInterfaceToStringConverter:

@Component
public class MyInterfaceToStringConverter<T extends MyInterface> implements Converter<T, String> {

    public String convert(T source) {
        if (source == null) {
            return null;
        }
        return source.getCode(); // This is a method in MyInterface which returns a String
    }
}

1 个答案:

答案 0 :(得分:1)

使用ConversionService配置的示例:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven conversion-service="conversionService" />

    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="myclasses.StringToMyInterfaceConverterFactory" />
                <bean class="myclasses.MyInterfaceToStringConverter" />
            </set>
        </property>
    </bean>
</beans>