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

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

问题描述

这个问题类似于:

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

但我对解决方案并不完全满意.上下文是相同的:我有一个需要整数值的网络表单.如果文本框留空,我希望我的 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 参数.

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.

推荐答案

您可以使用 System#setProperty().

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 注释.

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天全站免登陆