Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

jsf - How to set -Dorg.apache.el.parser.COERCE_TO_ZERO=false programmatically

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...