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

查看:182
本文介绍了如何使数字输入区域最初为空而不是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.0 >类型通过以下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天全站免登陆