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.

Community
  • 1
  • 1
Steve
  • 11,831
  • 14
  • 51
  • 63

1 Answers1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This is a good way to make the web application more compatible, keep confirguration inside itself make it easier! – Alexandre Lavoie Nov 09 '12 at 13:49
  • @AlexandreLavoie, it's also a good way to make the web application less compatible, since System#setProperty affects the whole JVM! – Vsevolod Golovanov Jun 20 '13 at 15:41
  • @this Wrong term, I was saying _portable_, anyway when playing with Integers or Longs it is a good think to have null instead of 0 – Alexandre Lavoie Jun 20 '13 at 18:21
  • if this still affects you and you need a portable solution, you can check this post: http://jdevelopment.nl/passing-null-model-jsf – Rafael Sisto Jul 03 '13 at 14:42
  • 1
    @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. – BalusC Jul 03 '13 at 14:46
  • Thanks, for your answer and @WebListener annotation. – MilanPanchal Jul 22 '13 at 13:03
  • 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. – SurendraKumar Jaiswal Dec 28 '18 at 13:36
  • As every post you write, is working and perfectly explained :) thanks balus! – C.B. Oct 05 '20 at 16:23