views:

319

answers:

1

Hi!

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.

A: 

When returning null in getAsObject(), you need to set the component's submitted value to null as well.

if (result == null) {
    ((EditableValueHolder) component).setSubmittedValue(null);
}

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() returns null. You can go around this by adding the following line to Tomcat's VM arguments:

-Dorg.apache.el.parser.COERCE_TO_ZERO=false
BalusC
Thank you for your reply. I'll give it a try.I'm using Websphere Application Server Community Edition, so I'll have to find out how to configure it there. Also, I have the same problem with String properties, for which I didn't include any converters.
Vladimir
JSF 1.1 or 1.2? If JSF 1.1, `String` converters are by design ignored. If JSF 1.2, do the same way.
BalusC
I'm using JSF 1.2. Unfortunately, I still haven't been able to start WAS CE with VM arguments.
Vladimir
No you can't pass those Tomcat specific arguments in WAS CE. It should however work, uses a modified version of an older Tomcat version under the hoods. For an `EmptyToNullConverter` for strings, check [this answer](http://stackoverflow.com/questions/2203322/hinputtext-return-a-empty-string-instead-of-null/2203871#2203871).
BalusC
Thank you once again for your answer. It provided solution for my problem.It turned out I managed to set VM arguments for WAS CE by using <WASCE_HOME>/bin/geronimosrvw.exe and by starting server through <WASCE_HOME>/bin/geronimosrv.exe. But, I still haven't been able to set it in any other way (Eclipse start or using start-server.bat).
Vladimir