问题
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