开发者

jsf: integer property binded to a inputtext in UI is set to zero on submit [duplicate]

开发者 https://www.devze.com 2023-02-01 03:33 出处:网络
This question already has an answer here: h:inputText which is bound to Integer property is submitting value 0 instead of null
This question already has an answer here: h:inputText which is bound to Integer property is submitting value 0 instead of null (1 answer) Closed 7 years ago.

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


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:

  • inputtext submitting value 0 instead of null
  • COERCE_TO_ZERO at runtime


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.


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消