How to standardize the use of convertible numbers in JSF for different components?

In my project I am using a lot of h: outputtext with f: convertnumber to apply the pattern to my numeric data.

<h:outputText  value="#{stock.price}">
    <f:convertNumber currencySymbol="" groupingUsed="true" maxFractionDigits="2" type="currency" />
</h:outputText>

      

Copying and pasting this template when needed for other data seems easy. But he is also uncontrollable; when used more, changing the template seems to require a lot of find / replace operations.

How to make this template reusable and manageable in a central location.

+3


source to share


1 answer


The easiest way is to create a custom converter that extends the desired standard converter and in which you set the desired default values ​​in the constructor.

@FacesConverter("defaultNumberConverter")
public class DefaultNumberConverter extends NumberConverter {

    public DefaultNumberConverter() {
        setCurrencySymbol("");
        setGroupingUsed(true);
        setMaxFractionDigits(2);
        setType("currency");
    }

}

      



Use it like this:

<h:outputText value="#{stock.price}" converter="defaultNumberConverter" />

      

+5


source







All Articles