If you use Spring 3 and yours is a web application, you need to:
- Write a custom formatter for the Boolean type:
Code:
public class MyCustomBooleanFormatter implements Formatter<Boolean> {
public String print (Boolean fieldValue, Locale locale) {
if (fieldValue.booleanValue()) return "Yes";
else return "No";
}
public Boolean parse (String clientValue, Locale locale) {
if ("Yes".equals(clientValue)) return new Boolean(true);
else if ("No".equals(clientValue)) return new Boolean(false);
else //throw some parsing exception or do what you like
}
}
- register this formatter using FormattingConversionServiceFactoryBean:
Code:
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.example.MyCustomBooleanFormatter"/>
</set>
</property>
</bean>
and that's it.