How to set -Dorg.apache.el.parser.COERCE_TO_ZERO=false programmatically
Asked Answered
G

1

25

This question is similar to:

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

but I am not completely satisfied with the solution. The contexts is the same: I have a web form requiring an Integer value. If the textbox is left empty, I want my Integer field to be 'null' but instead the EL Parser automatically sets my id field to '0'.

I can fix the problem by setting a JVM Parameter in my local Tomcat VM:

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

However, this will not work for our client's machine. Is it possible to set/change this JVM parameter "in-code".

Update: I've found that this is being requested but if anyone else has any other workaround, I would like to hear that too.

https://issues.apache.org/bugzilla/show_bug.cgi?id=48813

Update 2: I can't change the value back from a '0' to a 'null' because my application should treat '0' as an actual id. So I need to know at runtime whether the id textbox was left empty or not.

Gendarmerie answered 7/3, 2011 at 20:42 Comment(0)
G
48

You can set the system properties programmatically using System#setProperty().

System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");

However, you need to ensure that this is been set before JSF/EL ever get initialized. Best place would be a ServletContextListener.

public class Config implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // NOOP
    }

}

Register it as <listener> in web.xml, or when you're already on Servlet 3.0 (Tomcat 7 and so), with @WebListener annotation.

Gavrilla answered 7/3, 2011 at 20:47 Comment(8)
This is a good way to make the web application more compatible, keep confirguration inside itself make it easier!Flaming
@AlexandreLavoie, it's also a good way to make the web application less compatible, since System#setProperty affects the whole JVM!Charlenacharlene
@this Wrong term, I was saying portable, anyway when playing with Integers or Longs it is a good think to have null instead of 0Flaming
if this still affects you and you need a portable solution, you can check this post: jdevelopment.nl/passing-null-model-jsfIncomplete
@Rafael: That's an excellent article (written by my colleague :) ). We indeed applied it that way in a legacy JSF 1.2 originated production system where too many testing and changes were required after this change. It'd been better if it was set from the beginning on.Gavrilla
Thanks, for your answer and @WebListener annotation.Ratchford
Hello @BalusC, Currently in my project upgradation, I have upgraded java to 1.8 and tomcat to 8.5, But JSF is still 1.2. Earlier I was using ' -Dorg.apache.el.parser.COERCE_TO_ZERO=false' in tomcat7 argument, Now it stopped working and I am stuck. I also tried the above approach but it is not working. Any suggestion would be great help. Thank you in advance.Efik
As every post you write, is working and perfectly explained :) thanks balus!Hundredfold

© 2022 - 2024 — McMap. All rights reserved.