2017-01-30 102 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知识 –