JSF converter with null value
<p>I need to convert <code>null</code> property values of type <code>java.math.BigDecimal</code> to <code>0.00</code> everywhere with a specified number of decimal places (only for displaying (<code>&lt;h:outputText&gt;</code>) thus, not for input components).</p>

<p>The basic converter expected to do this job is given below (currency, percentage, locale etc have been excluded completely for brevity).</p>

<pre><code>@FacesConverter("bigDecimalConverter")
public final class BigDecimalConverter implements Converter {

    private static final int SCALE = 2;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {

        if (submittedValue == null || submittedValue.isEmpty()) {
            return null;
        }

        try {
            return new BigDecimal(submittedValue).setScale(SCALE, RoundingMode.HALF_UP).stripTrailingZeros();
        } catch (NumberFormatException e) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "Message"), e);
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object modelValue) {

        BigDecimal value;

        if (modelValue == null) { // This is expected to replace null with 0.
            value = BigDecimal.ZERO;
        } else if (modelValue instanceof Long) {
            value = BigDecimal.valueOf((Long) modelValue);
        } else if (modelValue instanceof Double) {
            value = BigDecimal.valueOf((Double) modelValue);
        } else if (!(modelValue instanceof BigDecimal)) {
            throw new ConverterException("Message");
        } else {
            value = (BigDecimal) modelValue;
        }

        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setGroupingUsed(true);
        numberFormat.setMinimumFractionDigits(SCALE);
        numberFormat.setMaximumFractionDigits(SCALE);
        return numberFormat.format(value);
    }
}
</code></pre>

<p>Referring to a property of the associated model or bean of type <code>java.math.BigDecimal</code> using output components like the following.</p>

<pre><code>&lt;h:outputText value="#{bean.value}"&gt;
    &lt;f:converter converterId="bigDecimalConverter"/&gt;
&lt;/h:outputText&gt;
</code></pre>

<p><code>bean.value</code> is a type of <code>java.math.BigDecimal</code> in a managed bean. If <code>bean.value</code> is <code>null</code>, then the <code>getAsString()</code> method is not invoked. Hence, an empty output is rendered where a value of zero is expected.</p>

<p>This <code>value="#{bean.value}"</code> needs to be changed to <code>value="#{empty bean.value ? 0 : bean.value}"</code> for the converter to perform its coherent task.</p>

<p>Putting this conditional test in EL everywhere is however, quite maintenance-unfriendly.</p>

<p>Is there a way to invoke the <code>getAsString()</code> method, when a target property is <code>null</code> which is conditionally attempted to be set to <code>0</code> in that method (<code>BigDecimal.ZERO</code>)?</p>

<hr>

<p><strong>Update :</strong></p>

<p>The <code>getAsString()</code> method is invoked with <code>&lt;o:param&gt;</code> (OmniFaces).</p>

<pre><code>&lt;h:outputFormat value="Discount ({0}%)"&gt;
    &lt;o:param value="#{bean.value}"&gt;
        &lt;f:converter converterId="bigDecimalConverter"/&gt;
    &lt;/o:param&gt;
&lt;/h:outputFormat&gt;
</code></pre>

<p>This displays <code>Discount (0.00%)</code> after has been converted by the converter, when <code>#{bean.value}</code> returns <code>null</code>.</p>