查看BigDecimal时的十进制分隔符

时间:2017-01-30 11:21:35

标签: jsf jsf-2.2

我有一个包含String和BigDecimal类型字段的类。 当显示BigDecimal作为分隔符时,我想要成为逗号。我改变了波兰语的编码,但仍然是点。 当我使用<f:convertNumber locale=""#{language.locale}"/>工作正常,但不想在每个领域都这样做,因为我有很多这样做

实现这一目标的最佳方法是什么?

面-config.xml中

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <application>
        <locale-config>
            <default-locale>pl</default-locale>
        </locale-config>
    </application>
</faces-config>

Language.java

@Named
@SessionScoped
public class Language implements Serializable {

    private Locale locale;

    @PostConstruct
    public void init() {
        locale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
    }

    public Locale getLocale() {
        return locale;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

}

view.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui"
      xmlns:fn="http://xmlns.jcp.org/jsp/jstl/functions">
    <f:view locale="#{language.locale}">
        <h:head>
            <f:facet name="first">
                <meta http-equiv="X-UA-Compatible" content="IE=edge" />
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
                <h:outputScript library="primefaces" name="jquery/jquery.js" />
            </f:facet>
            <f:facet name="middle">
                <h:outputStylesheet library="css" name="bootstrap.css" />
                <h:outputStylesheet library="css" name="default.css" />
                <h:outputScript name="js/bootstrap.js" />
            </f:facet>   
        </h:head>
        <h:body>
            <h:form id="menuForm">
                ...
                ...
                ...
                <h:outputText value="#{detail.1}" />    // string
                <h:outputText value="#{detail.2}" />    // bigdecimal
                ...
                ...
                ...
            </h:form>
        </h:body>
    </f:view>
</html>

1 个答案:

答案 0 :(得分:0)

您需要以某种方式转换为您的语言环境

  

当我使用<f:convertNumber locale=""#{language.locale}"/>时工作正常,但不想在每个领域都这样做,因为我有很多这样做

因此,如果您不想在客户端执行此操作,则需要向bean添加getLocaleNumber()(或类似)方法,以便以所需格式显示数字:

public String getLocalized(BigDecimal number) throws Exception {
    // get desired locale (maybe a constant will suit you better)
    NumberFormat nf = NumberFormat.getInstance(new Locale("pl_PL"));
    return nf.format(number.doubleValue());
}