How to standardize convertnumber usage in JSF for various components?

允我心安 提交于 2019-12-30 10:30:14

问题


In my project I use lots of h:outputtext with f:convertnumber to apply a 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 pattern when needed for other data seems to be easy. But it is also unmanageable; when used more, changing the pattern seems to require lots of search/replace operations.

How can I make this pattern be reusable and managed in a central location.


回答1:


Easiest is to create a custom converter which extends the desired standard converter and wherein you set the desired defaults in the constructor.

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

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

}

Use it as follows:

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


来源:https://stackoverflow.com/questions/14772127/how-to-standardize-convertnumber-usage-in-jsf-for-various-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!