Question

I have a Spring 3 MVC app utilizing <mvc:annotation-driven />. Having trouble getting a java.lang.Double property to display as currency in a <form:input> tag. The amount shows correctly, but no formatting applied. Any guidance?

Spring config:

<mvc:annotation-driven conversion-service="conversionService">
    <mvc:argument-resolvers> 
        <bean class="com.my.SessionUserHandlerMethodArgumentResolver"/>         
    </mvc:argument-resolvers>
</mvc:annotation-driven> 

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="com.my.StringToDoubleConverter" />
        </list>
    </property>
</bean>

Domain Entity field annotaion:

import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;

    @Entity
    @Table(name="form_data_wire_transfers")
    @PrimaryKeyJoinColumn(name="form_id")
    public class WireRequestForm extends RequestForm {

        private Double amount;

        @Column(name="amount")
        @NumberFormat(style=Style.CURRENCY)
        public Double getAmount() {
            return amount;
        } 
        public void setAmount(Double amount) {
            this.amount = amount;
        }

    }

Controller Method:

@RequestMapping(value="/forms/{formId}", method=RequestMethod.GET)
    public String show(Model uiModel, @PathVariable("formId") Integer formId){

        RequestForm form = formService.findOne(formId);

        uiModel.addAttribute("form", form);

        return "show/form";
    }

View:

<form:form modelAttribute="form" action="${saveFormUrl}" method="POST">
    <!-- AMOUNT -->
    <div>
        <form:label path="amount">Amount</form:label>
        <form:input path="amount" />
    </div>
</form:form>

Again, I see the value, but it display like this 1111.0 versus $1,111.00.

Was it helpful?

Solution

ConversionServiceFactoryBean does not registers default formatters.

You need to use FormattingConversionServiceFactoryBean
So do as below

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="com.my.StringToDoubleConverter" />
        </list>
    </property>
</bean>



if you want to use only NumberFormatAnnotationFormatterFactory which does the Number Formatting (processes @NumberFormat Annotation ) and disable its other default formatters then do as below

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="registerDefaultFormatters" value="false" />
    <property name="formatters">
    <set>
        <bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" />
    </set>
    </property>
     <property name="converters">
    <list>
        <bean class="com.my.StringToDoubleConverter" />
    </list>
</property>
</bean>

Source:Spring Docs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top