How to make number input area initially empty instead of 0 or 0.00?

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:

Leave a Comment