Using spring web flow 2, Formatter by field type is effective but Formatter for Field Annotation is not.
getPrint and getParser not called. (by field type, they are called)
I've spent much time about this, but have no good results.
Bean for page
public TestBean {
@TestFormat
private String test;
...
}
Annotation
@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();
}
}
Formatter
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 Configuration
<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 Configuration
<bean id="defaultConversionService" class="org.springframework.binding.convert.service.DefaultConversionService" >
<constructor-arg ref="applicationConversionService"/>
</bean>
<webflow:flow-builder-services id="flowBuilderServices" conversion-service="defaultConversionService"/>
this might be related, but I could not find a solution
Spring Web Flow 2.4.1
Spring 4.1.6
Thymeleaf 2.1.4
When implementing the
AnnotationFormatterFactory
then itsgetFieldTypes
method should return the type of the fields the annotation applies to. With your current configuration you are sayingTestFormat
can be annotated withTestFormat
.I suspect however that you want to specify
String
can be annotated withTestFormat
.Change your implementation to return
String.class
instead.