我正在使用Spring webflow。
我配置了FormattingConversionService
。在此转换服务中,我配置了以下内容:
ConverterFactory
将字符串值转换为MyInterface
个实例(绑定到对象)Converter
将MyInterface
的对象转换为字符串(用于显示)调用'ConverterFactory`并完美运行。
我的问题是没有调用Converter
。 toString()
显示在页面上。
如何让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
}
}
答案 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>