如何使数字输入区域最初为空而不是 0 或 0.00? [英] How to make number input area initially empty instead of 0 or 0.00?

查看:25
本文介绍了如何使数字输入区域最初为空而不是 0 或 0.00?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该得到一个数字的输入位置.我希望它显示为空.但是,当我运行程序时,默认值为 0 或 0.00.我怎样才能让它为空?

I have an input place that should get a number. I want it to be displayed as empty. However when I run my program, I get 0 or 0.00 as default. How can I make it empty?

推荐答案

如果您将值绑定到一个原语而不是它的包装表示,就会发生这种情况.原语int 总是默认为0,而原语double 总是默认为0.0.您想改用 IntegerDouble(或 BigDecimal).

This will happen if you bound the value to a primitive instead of its wrapper representation. The primitive int always defaults to 0 and the primitive double always defaults to 0.0. You want to use Integer or Double (or BigDecimal) instead.

例如:

public class Bean {

    private Integer number;

    // ...
}

那么在处理表单提交时还有两件事需要考虑.首先,需要指示JSF将提交的空字符串解释为null,否则EL仍然会将空字符串强制为00.0.这可以通过 web.xml 中的以下上下文参数来完成:

Then there are two more things to take into account when processing the form submit. First, you need to instruct JSF to interpret empty string submitted values as null, otherwise EL will still coerce the empty string to 0 or 0.0. This can be done via the following context parameter in web.xml:

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

其次,如果您使用的是 Tomcat/JBoss 或任何在幕后使用 Apache EL 解析器的服务器,那么您需要指示它不要将 null 强制转换为 00.0Number 类型的情况下通过以下 VM 参数(它不直观地处理 Number 类型,就好像它们是原始类型一样):

Second, if you're using Tomcat/JBoss or any server which uses Apache EL parser under the covers, then you need to instruct it to not coerce null to 0 or 0.0 in case of Number types by the following VM argument (it's unintuitively dealing with Number types as if they are primitives):

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

另见:

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