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?
4 Answers
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.
E.g.:
public class Bean {
private Integer number;
// ...
}
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>
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
See also:
- 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
-
Hi @BalusC, what file do I need to add the VM argument to and where is it located? – Erick Oct 20 '18 at 17:28
-
@Erick: It needs to go in the `java` launch command of the server. It's not necessarily in a file and depends on how you're launching the server. – BalusC Oct 21 '18 at 15:20
I don't disagree with previous answers given, but I think you might be looking for
<h:inputText id="number" binding="#{controller.inputBox}" value="#{controller.number}" >
<f:event type="preRenderComponent" listener="#{controller.preRenderInput}" />
</h:inputText>
and inside the controller
private double number; // getters and setters
private UIInput inputBox; // getters and setters
public void preRenderInput() {
// if it is the first request
if(!FacesContext.getCurrentInstance().isPostback()) {
inputBox.setValue("");
}
}
JSF 2.0+

- 21
- 3
You can use inputNumber in primefaces-extension instead of inputText. Like this
<pe:inputNumber />
In your XHTML code, assume you have your input field as :
<h:inputText id="newname" value="#{youAction.newName}"
style="width:130px" styleClass="form">
</h:inputText>
In your Managed Bean Action Class, map this field as :
@ManagedBean(name="yourAction")
public class YourAction {
private String newName;
...
// Add Getter & Setter Methods
...
}
By doing in this way, you won't get 0 or 0.00 as default value in your input fields.

- 2,604
- 6
- 26
- 36
-
my question is about numeric values, I do not have any problem with strings. But I still have the problem for integers and doubles. – Gary Leather Jan 15 '13 at 09:40
-
@GaryLeather - When you want to make use of the value, why don't you convert the value to the type you want. – Venkat Jan 15 '13 at 10:20