【问题标题】:Spring Formatter for field annotation not work in Web Flow用于字段注释的 Spring Formatter 在 Web Flow 中不起作用
【发布时间】:2015-11-20 17:27:32
【问题描述】:

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

getPrintgetParser 未调用。 (按字段类型,它们被称为)

我为此花了很多时间, 但没有好的结果。

页面 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


【解决方案1】:

当实现AnnotationFormatterFactory 时,它的getFieldTypes 方法应该返回注释应用到的字段的类型。使用您当前的配置,您说 TestFormat 可以用 TestFormat 进行注释。

不过我怀疑你想指定 String 可以用 TestFormat 注释

更改您的实现以返回 String.class

public class TestFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<TestFormat>,Serializable {
    @Override
    public Set<Class<?>> getFieldTypes() {
        return Collections.singleton(String.class);
    }
    ...
}

【讨论】:

    猜你喜欢
    • 2017-09-01
    • 2014-08-14
    • 2012-07-29
    • 2015-10-26
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    相关资源
    最近更新 更多