问题描述
我有一个应该输入数字的输入位置.我希望它显示为空.但是,当我运行程序时,默认值为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
>类型通过以下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自Java EE 7/EL 3.0起,.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL不再起作用
- The empty string madness
- h:inputText which is bound to Integer property is submitting value 0 instead of null
- h:inputText which is bound to String property is submitting empty string instead of null
- javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL does not work anymore since Java EE 7 / EL 3.0
See also:
这篇关于如何使数字输入区域最初为空而不是0或0.00?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!