【发布时间】:2015-11-20 17:27:32
【问题描述】:
使用spring web flow 2, 按字段类型格式化是有效的 但字段注释的格式化程序不是。
getPrint 和 getParser 未调用。 (按字段类型,它们被称为)
我为此花了很多时间, 但没有好的结果。
页面 Bean
public TestBean {
@TestFormat
private String test;
...
}
注释
@Target({ElementType.TYPE,ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestFormat {}
AnnotationFormatterFactory
public class TestFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<TestFormat>,Serializable {
@Override
public Set<Class<?>> getFieldTypes() {
Set<Class<?>> set = new HashSet<Class<?>>();
set.add(TestFormat.class);
return set;
}
@Override
public Printer<?> getPrinter(TestFormat annotation, Class<?> fieldType) {
return new TestFormatter();
}
@Override
public Parser<?> getParser(TestFormat annotation, Class<?> fieldType) {
return new TestFormatter();
}
}
格式化程序
public class TestFormatter implements Formatter<String>{
@Override
public String print(String str, Locale locale) {
return str.substring(0, str.indexOf("parsed")); // example
}
@Override
public String parse(String input, Locale locale) throws ParseException {
return input + "parsed"; // example
}
}
ApplicationFormatterRegistrar
public class ApplicationFormatterRegistrar implements FormatterRegistrar {
@Override
public void registerFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldAnnotation(new TestFormatAnnotationFormatterFactory());
}
}
SpringMVC 配置
<bean id="applicationConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatterRegistrars">
<set>
<ref local="applicationFormatterRegistrar"/>
</set>
</property>
</bean>
<bean id="applicationFormatterRegistrar" class="package.ApplicationFormatterRegistrar"/>
Spring Webflow 配置
<bean id="defaultConversionService" class="org.springframework.binding.convert.service.DefaultConversionService" >
<constructor-arg ref="applicationConversionService"/>
</bean>
<webflow:flow-builder-services id="flowBuilderServices" conversion-service="defaultConversionService"/>
这可能是相关的,但我找不到 solution
Spring Web Flow 2.4.1
Spring 4.1.6
Thymeleaf 2.1.4
【问题讨论】:
-
抱歉我换行不好。
-
您的
getFieldTypes应该将String.class添加到列表中,而不是TestFomat,目前它没有。表示可以注解的字段类型,目前你说TestFormat可以用TestFormat注解。而你想要String可以用TestFormat注释。 -
成功了!我错过了。感谢您的准确指示。
标签: java spring spring-webflow spring-webflow-2