字段注释的Spring Formatter在Web Flow

时间:2015-06-22 06:36:52

标签: java spring spring-webflow spring-webflow-2

使用spring web flow 2, 按字段类型格式化是有效的 但字段注释的格式化程序不是。

未调用

getPrint getParser 。 (按字段类型,它们被称为)

我花了很多时间这个, 但没有好结果。

Bean for page

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

1 个答案:

答案 0 :(得分:0)

实现getFieldTypes时,其TestFormat方法应返回注释所适用的字段类型。根据您当前的配置,您说 TestFormat可以使用String 进行注释。

我怀疑您要指定 TestFormat可以使用String.class 进行注释。

将实施更改为返回public class TestFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<TestFormat>,Serializable { @Override public Set<Class<?>> getFieldTypes() { return Collections.singleton(String.class); } ... }

public class MyLovelyClass
{
    public Int32 Number { get; set; }
    public bool Selection { get; set; }
}

var packs = from r in new XPQuery<Roll>(session)
            select new MyLovelyClass()
            {
               Number = r.number                
            };
gcPack.DataSource = packs;