如何以编程方式设置-Dorg.apache.el.parser.COERCE_TO_ZERO = false [英] How to set -Dorg.apache.el.parser.COERCE_TO_ZERO=false programmatically

查看:108
本文介绍了如何以编程方式设置-Dorg.apache.el.parser.COERCE_TO_ZERO = false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似于:

jsf:在UI上绑定到输入文本的整数属性在提交时设置为零

但是我对解决方案并不完全满意.上下文是相同的:我有一个Web表单,需要一个Integer值.如果文本框为空,我希望我的Integer字段为"null",但是EL Parser会自动将我的id字段设置为"0".

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'.

我可以通过在本地Tomcat VM中设置JVM参数来解决此问题:

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

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

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

但是,这不适用于我们客户的计算机.是否可以设置/更改此JVM参数"in-code".

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

更新2:我无法将值从"0"改回"null",因为我的应用程序应将"0"视为实际ID.因此,我需要在运行时知道id文本框是否为空.

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.

推荐答案

您可以使用

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

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

但是,您需要确保在初始化JSF/EL之前 设置了此设置.最好的地方是ServletContextListener.

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
    }

}

web.xml中或当您已经在Servlet 3.0(Tomcat 7等)上使用@WebListener批注将其注册为<listener>.

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

这篇关于如何以编程方式设置-Dorg.apache.el.parser.COERCE_TO_ZERO = false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆