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

前提是你 提交于 2019-11-28 10:06:53
meriton

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.

BalusC

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:

Anthony O.

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.

Klaus Schuster

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!