I’m new to JSF, so this question might be strange. I have an inputText component’s value bound to managed bean’s property of type Float. I need to set property to null when inputText field is empty, not to 0 value. It’s not done by default, so I added converter with the following method implemented:
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {
if (StringUtils.isEmpty(arg2)) {
return null;
}
float result = Float.parseFloat(arg2);
if (result == 0) {
return null;
}
return result;
}
I registered converter, and assigned it to inputText component. I logged arg2 argument, and also logged return value from getAsObject method. By my log I can see that it returns null value. But, I also log setter property on backing bean and argument is 0 value, not null as expected. To be more precise, it is setter property is called twice, once with null argument, second time with 0 value argument.
It still sets backing bean value to 0. How can I set value to null?
Thanks in advance.
When returning
nullingetAsObject(), you need to set the component’s submitted value tonullas well.There is however an environment specific issue with this, namely the fact that the EL parser in Tomcat 6.0.16 or newer will still coerce this value as 0 in the view side even though when
getAsString()returnsnull. You can go around this by adding the following line to Tomcat’s VM arguments: