Announcement Announcement Module
Collapse
No announcement yet.
Custom annotated formatter not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom annotated formatter not working

    I've created a custom annotated formatter as per the spring docs but have had no luck getting it to actually format the field on a form. It seems to ignore the formatter, yet I'm also using the built-in DateFormat on another field fine. I'm using Spring 3.0.5.

    Command object:
    Code:
    public class UpdateRuleForm {	
    	@RuleTitleFormat private String title;
    	@DateTimeFormat(pattern="dd/MM/yy HH:mm") private Date availableStart;
    ..
    }
    Annotation:
    Code:
    @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface RuleTitleFormat {
    }
    Formatter:
    Code:
    public class RuleTitleFormatter implements Formatter<String> {
    	@Override
    	public String print(String text, Locale locale) {
    		return text.replaceAll("a", ",");
    	}
    	@Override
    	public String parse(String text, Locale locale) throws ParseException {
    		return text.replaceAll(",", "\\s");
    	}
    }
    AnnotationFormatterFactory:
    Code:
    public class RuleTitleFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<RuleTitleFormat> {
    	@Override
    	public Set<Class<?>> getFieldTypes() {
    		Set<Class<?>> rawFieldTypes = new HashSet<Class<?>>(1);
    		rawFieldTypes.add(String.class);
    		return rawFieldTypes;
    	}
    	@Override
    	public Printer<?> getPrinter(RuleTitleFormat annotation, Class<?> fieldType) {
    		return new RuleTitleFormatter();
    	}
    	@Override
    	public Parser<?> getParser(RuleTitleFormat annotation, Class<?> fieldType) {
    		return new RuleTitleFormatter();
    	}
    }
    FormattingConversionServiceFactoryBean:
    Code:
    public class ConfigurableFormattingConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {
        @Override
        protected void installFormatters(FormatterRegistry registry) {
            super.installFormatters(registry);
            registry.addFormatterForFieldAnnotation(new RuleTitleFormatAnnotationFormatterFactory());
        }  
    }
    Application Context:
    Code:
    <mvc:annotation-driven conversion-service="conversionService" />
    <bean id="conversionService" class="com.xyz.ConfigurableFormattingConversionServiceFactoryBean" />
    <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
    <context:spring-configured/>
    <context:component-scan base-package="com.xyz">
            <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>
    ...
    JSPX:
    Code:
    <jsp:root xmlns:spring-form="http://www.springframework.org/tags/form" ..>
    ...
    <spring-form:input max="40" size="50" path="title" />
    Any ideas what I'm missing? FYI, this is a Spring ROO generated app.
Working...
X