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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Averroes
  • 4,168
  • 6
  • 50
  • 63
  • **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 `` and our component kept returning 0 on empty input value. It turned out, `emptyValue` attribute on `` caused that issue (even when it was empty - e.g. `emptyValue=""`). After having removed `emptyValue` attribute (stopped declaring it in ``) everything works fine. – AndrewMcCoist Feb 16 '18 at 13:18

1 Answers1

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • And there is no way around this except to turn it off/on globally? Also with the context-param entry? Congrats to 500k btw ;) – dasLort Aug 14 '14 at 08:22
  • @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: http://stackoverflow.com/questions/5225013/coerce-to-zero-at-runtime/5225055#5225055 – BalusC Aug 14 '14 at 08:24
  • 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 http://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html but did not see the location to make the entry. Thank you. – Ted Spradley Sep 05 '14 at 18:58
  • 1
    @Ted: depends on how you start Tomcat. E.g. IDE, commandline, service, etc. That argument has basically to be passed to `java` command. – BalusC Sep 05 '14 at 20:06
  • @BalusC 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. – Ted Spradley Sep 06 '14 at 00:10