如何使数字输入区域最初为空而不是 0 或 0.00? [英] How to make number input area initially empty instead of 0 or 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
.您想改用 Integer
或 Double
(或 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仍然会将空字符串强制为0
或0.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
强制转换为 0
或 0.0
在 Number
类型的情况下通过以下 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
另见:
- 空字符串的疯狂
- h:inputText绑定到 Integer 属性的提交值是 0 而不是 null
- h:inputText绑定到 String 属性正在提交空字符串而不是 null
- javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL 自 Java EE 7/EL 3.0 起不再起作用
这篇关于如何使数字输入区域最初为空而不是 0 或 0.00?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!