9

I have integer properties from my bean binded to inputtext UI elements in jsp pages.

initially when they are rendered, default value of this integer properties is null.

Now when i am submitting the form without changing this inputtext fields, this fields are set to integer value zero in bean, even though ui text field is blank.

As i want to track the changes to fields and update only those fields which are changed in configuration files, but this is giving me problem as it results into updates to all integer field.

I am using jsf 1.2

changed
  • 2,103
  • 8
  • 36
  • 56

5 Answers5

18

This issue is however specific to EL implementation of Tomcat (Glassfish for example, doesn't expose this stupid behaviour). It used to work "as intuitively expected" until Tomcat 6.0.16. Then they discovered that it actually violated the literal EL spec and fixed it. After a lot of critism, they made it configureable from 6.0.17 and upwards. You can turn it off by adding the following VM argument:

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

This is IMO better than hacking into getters/setters. You don't want to pollute your model like that.

Related questions:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 3
    I don't want to pollute my model either. However I once used COERCE_TO_ZERO, and the integration people promptly forgot to set it when they deployed in the next staging environment, causing more wasted effort all around than a fix in code would have. – meriton Dec 28 '10 at 13:17
  • so this configuration is for Tomcat only? I'm using Websphere and also encountering this problem – Fritz Oct 24 '14 at 08:25
  • Note for those that don't want to change this in the environment, you can use [this](http://stackoverflow.com/questions/5225013/coerce-to-zero-at-runtime/5225055#5225055) to get around it. – Addison Sep 09 '15 at 23:11
9

The JSF EL specification decrees that null is to be converted to 0 prior to assigning a property of numeric type. (See the chapter on coercion rules). An issue has been filed about this, but is being ignored by the spec people.

There are no really pretty solutions. The easiest is to convert 0 back to null in the setter, but that assumes 0 is never a valid input. Other alternatives include having the setter and getter receive/return a non-numeric type such as String, and do the conversion to/from Integer in the setter/getter. That however means you would detect non-numeric inputs too late in the JSF life cycle, so you also need an additional validator/converter to handle that.

Edit: Websphere 7.0.0.11 doesn't coerce null to 0.

Community
  • 1
  • 1
meriton
  • 68,356
  • 14
  • 108
  • 175
2

See that very complete answer by BalusC.

He says you can set the system property org.apache.el.parser.COERCE_TO_ZERO to false, but you can also write a ServletContextListener which would set it when your webapp starts.

Community
  • 1
  • 1
Anthony O.
  • 22,041
  • 18
  • 107
  • 163
1

For Websphere Users, define an user defined JVM System Property, and it works!

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • can you be a bit more specific on this? what specific JVM system property did you use to address this problem in websphere? – Fritz May 08 '15 at 02:28
0

Found this link in the IBM support website for Websphere users.

Basically here are the steps:

You set the org.apache.el.parser.COERCE_TO_ZERO property using the administrative console as follows:

  1. Expand Servers, select WebSphere Application Servers, and then click on the appropriate server from the list.

  2. Under Server Infrastructure, expand Java and Process Management > Click on Process definition.

  3. Under Additional Properties, click Java virtual Machine.

  4. Under Additional Properties, click Custom properties.

  5. Click New and add the org.apache.el.parser.COERCE_TO_ZERO property with the value of false if you do NOT want a null value coerced to zero.

  6. Click Save to save the change and restart the WebSphere

Application Server to ensure the change takes place. Default value: true

Fritz
  • 1,144
  • 1
  • 13
  • 21