1
使用弹簧web流程2, 根据字段类型的格式化程序有效 但字段注释的格式程序不是。用于字段注释的弹簧格式化程序在Web流程中不起作用
getPrint and getParser not called。 (按字段类型,他们被称为)
我已经花了很多时间在这个, 但没有好的结果。
豆的页面
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"/>
春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
对不起我的坏的换行符。 – user1451619
你的['getFieldTypes'](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/format/AnnotationFormatterFactory.html#getFieldTypes--)应该添加'String.class'到该列表而不是'TestFomat',目前它没有。它表示可以注释的字段的类型,目前你说'TestFormat'可以用'TestFormat'注释。而你想'String'可以用'TestFormat'注释。 –
它的工作! 我错过了。 谢谢您的准确指示。 – user1451619