2017-01-30 24 views
0

我有一個包含String和BigDecimal類型字段的類。 將BigDecimal作爲分隔符顯示時,我想成爲一個逗號。我改變了波蘭的編碼,但仍然是點。 當我使用<f:convertNumber locale=""#{language.locale}"/>工作正常,但不想在每個領域這樣做,因爲我有很多他們查看BigDecimal時的小數分隔符

什麼是實現這一目標的最佳方式?

faces-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> 

回答

0

你需要轉換爲您的區域以某種方式

當我使用<f:convertNumber locale=""#{language.locale}"/>工作正常,但並不想這樣做,在每一個領域,因爲我有很多他們的

所以,如果你不」不想做的客戶端,您將需要一個getLocaleNumber()(或類似)的方法,以便在您需要的格式的號碼添加到你的bean:

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()); 
} 
+0

@BalusC其實我沒有搜索在谷歌這個答案,我只給OP *我使用規範的解決方案盟友*,也許四捨五入失蹤,但國際海事組織的做法在bean中的格式化getter是正確的方式去,如果你不想重複使用'',如果沒有,請加入你的方法,我相信你們的會更好,但對不起,我不同意在黑暗中拍攝,如果錯誤是缺乏我身邊的JSF知識 –