h:inputText which is bound to Integer property is submitting value 0 instead of null
Asked Answered
T

1

17

We are using a h:inputText in a JSF page which is bound to an Integer property (and thus can accept null). When there is no value written in the h:inputText, the form is submitting a 0 instead of null. We are using Trinidad 1.2.2 and Tomcat 6.0.20 (we also tried with Tomcat 6.0.14 as we read that this could happen with certains Tomcat versions).

How is this caused and how can I solve it?

Thrombin answered 25/6, 2010 at 8:17 Comment(1)
PrimeFaces specific: Been fighting with this problem for around a day. In our project, we have a custom tag library overriding PrimeFaces 6.0 components to match our needs and styles. We have a tag overriding <p:inputNumber/> and our component kept returning 0 on empty input value. It turned out, emptyValue attribute on <p:inputNumber/> caused that issue (even when it was empty - e.g. emptyValue=""). After having removed emptyValue attribute (stopped declaring it in <p:inputNumber/>) everything works fine.Dagny
M
27

This "feature" was result of a bugfix in EL which was introduced as per Tomcat 6.0.16. As per chapter 1.18.3 of the EL specification, a value of number type which is null should be coerced to 0. Before Tomcat 6.0.16 this was "incorrectly" been coerced to an empty string.

After all, an empty string was actually much more intuitive than a zero. The Tomcat guys at Apache got a lot complaints about this bugfix, so they introduced a new configuration setting in flavor of a VM argument as per Tomcat 6.0.17 which would disable this bugfix.

-Dorg.apache.el.parser.COERCE_TO_ZERO=false

I've by the way requested a change to this spec that it should only coerce empty/null primitive types to zero and empty/null non-primitive java.lang.Number types to empty string: JSP EL issue 184. They did not much with it, but the issue at least got a lot of votes. Who knows... This behaviour is at least indeed pretty annoying because it's non-intuitive.

See also:

Melly answered 25/6, 2010 at 11:28 Comment(5)
And there is no way around this except to turn it off/on globally? Also with the context-param entry? Congrats to 500k btw ;)Piercy
@das: It's a server setting, not a webapp setting. So context param is definitely out of question. Closest what you can do from webapp side on is to set the VM argument programmatically by System#setProperty() and pray that this isn't already being consulted before you set it programmatically (it will namely be consulted only once). Doing so in a ServletContextListener should be sufficient: https://mcmap.net/q/528007/-how-to-set-dorg-apache-el-parser-coerce_to_zero-false-programmatically/…Melly
Is the VM (Virtual Machine?) argument "-Dorg.apache.el.parser.COERCE_TO_ZERO=false" entered into the /tomcathome/conf/system.properties file? I find the settings at tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html but did not see the location to make the entry. Thank you.Ozzie
@Ted: depends on how you start Tomcat. E.g. IDE, commandline, service, etc. That argument has basically to be passed to java command.Melly
@Melly Thank you. For Eclipse IDE I found it under Server->Tomcat->Open Launch Configuration->Arguments. For my production server I suppose I'll add it at the command line.Ozzie

© 2022 - 2024 — McMap. All rights reserved.