2014-02-20 19 views
1

IM使用primefaces數據表顯示在這裏我的數據 是我的代碼:着顯示靜態int變量在primefaces數據表

<p:dataTable id="tab6" var="comp" value="#{excelReport.report6}" rendered="#{excelReport.date != null}"> 
        <f:facet name="header"> 
         <h:outputText value="heading text"/> 
        </f:facet> 
        <p:columnGroup type="header"> 
         <p:row> 
          <p:column headerText="Sr No" /> 
          <p:column headerText="Years" /> 
          <p:column headerText="Quarter no." /> 
          <p:column headerText="POL" /> 
          <p:column headerText="LPG" /> 
          <p:column headerText="AVIATION" /> 
          <p:column headerText="LUBES" /> 
          <p:column headerText="OTHERS" /> 
         </p:row> 
        </p:columnGroup> 
        <p:column><h:outputText value="#{comp.serialNo}" /> </p:column> 
        <p:column><h:outputText value="#{comp.quarterRange}" /></p:column> 
        <p:column><h:outputText value="#{comp.quarterNumber}" /></p:column> 
        <p:column><h:outputText value="#{comp.pol}" /></p:column> 
        <p:column><h:outputText value="#{comp.lpg}" /></p:column> 
        <p:column><h:outputText value="#{comp.aviation}" /></p:column> 
        <p:column><h:outputText value="#{comp.lubes}" /></p:column> 
        <p:column><h:outputText value="#{comp.others}" /></p:column>  
        <p:columnGroup type="footer"> 
         <p:row> 
          <p:column footerText="Total" colspan="3"/> 
          <p:column footerText="#{comp.polTotal}"/> 
          <p:column footerText="#{comp.lpgTotal}"/> 
          <p:column footerText="#{comp.aviationTotal}"/> 
          <p:column footerText="#{comp.lubesTotal}"/> 
          <p:column footerText="#{comp.othersTotal}"/> 
         </p:row> 
        </p:columnGroup> 
       </p:dataTable> 

每一件事工作正常,除了我不會在我的頁腳行得到任何數據。 我想在我的footerText中顯示int類型的靜態變量。

這裏是豆:

package dao; 

import java.io.Serializable; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 

@ManagedBean 
@ViewScoped 
public class Excel_Target_Compliance implements Serializable { 

    public Excel_Target_Compliance() { 
    } 
    private String quarterRange; 
    private String quarterNumber; 
    private int pol; 
    private int lpg; 
    private int aviation; 
    private int lubes; 
    private int others; 
    private int serialNo; 
    static int polTotal; 
    static int lpgTotal; 
    static int aviationTotal; 
    static int lubesTotal; 
    static int othersTotal; 

    public static int getPolTotal() { 
     return polTotal; 
    } 

    public static int getLpgTotal() { 
     return lpgTotal; 
    } 

    public static int getAviationTotal() { 
     return aviationTotal; 
    } 

    public static int getLubesTotal() { 
     return lubesTotal; 
    } 

    public static int getOthersTotal() { 
     return othersTotal; 
    } 

    public int getSerialNo() { 
     return serialNo; 
    } 

    public void setSerialNo(int serialNo) { 
     this.serialNo = serialNo; 
    } 

    public String getQuarterRange() { 
     return quarterRange; 
    } 

    public void setQuarterRange(String quarterRange) { 
     this.quarterRange = quarterRange; 
    } 

    public String getQuarterNumber() { 
     return quarterNumber; 
    } 

    public void setQuarterNumber(String quarterNumber) { 
     this.quarterNumber = quarterNumber; 
    } 

    public int getPol() { 
     return pol; 
    } 

    public void setPol(int pol) { 
     polTotal = polTotal + pol; 
     this.pol = pol; 
    } 

    public int getLpg() { 
     return lpg; 
    } 

    public void setLpg(int lpg) { 
     lpgTotal = lpgTotal + lpg; 
     this.lpg = lpg; 
    } 

    public int getAviation() { 
     return aviation; 
    } 

    public void setAviation(int aviation) { 
     aviationTotal = aviationTotal + aviation; 
     this.aviation = aviation; 
    } 

    public int getLubes() { 
     return lubes; 
    } 

    public void setLubes(int lubes) { 
     lubesTotal = lubesTotal + lubes; 
     this.lubes = lubes; 
    } 

    public int getOthers() { 
     return others; 
    } 

    public void setOthers(int others) { 
     othersTotal = othersTotal + others; 
     this.others = others; 
    } 
} 
+0

哪裏是你的polTotal消氣? – Makky

+0

在同一個豆上面我只是沒有顯示檢查againg我編輯了豆上面 – Sumit

+0

發佈您的全班。 – Makky

回答

3

其他答案已經提到,您不能使用EL表達式以這種方式訪問​​靜態字段。

但我認爲你真正的問題是關於面向對象的概念。我沒有理由在Excel_Target_Compliance中使用static。這甚至是不好的做法。您的bean是視圖範圍的,因此您將擁有多個實例(每個視圖顯示一個實例),但靜態字段對於整個應用程序是唯一的。因此,只要兩個用戶訪問同一頁面,您就會遇到併發問題。

就你而言,我會創建兩個類:一個表示表中的一行,一個表示整個表,並且可以返回總信息。這可能是這樣的(簡化):

public class ComplianceTable { 

    private List<ComplianceLine> lines; 

    public int getPolTotal() { 
     // ... go through all lines to get the total 
    } 

    // other methods currently represented as static fields 

} 

public class ComplianceLine { 
    private int pol; 
    // ... other fields from Excel_Target_Compliance 
} 

在JSF的代碼,你必須引用lines元素爲p:dataTable

<p:dataTable value="#{complianceTable.lines}" ...> 
+0

做得很好,但請注意,EL-3.0允許靜態引用(另請參閱:http://stackoverflow.com/a/3735006/785663) – mabi

+0

@mabi有趣!謝謝。我不知道。 – LaurentG

1

從我的理解,一個靜態變量不屬於實例,而是包含變量的類。我不認爲你可以直接從JSF訪問靜態變量。相反,您可以使用其getter方法創建另一個變量,以便像下面那樣獲取靜態變量的值。

public int getTotalPol(){ 
    return Bean.polTotal; 
} 

並在JSF頁面上使用此方法來訪問靜態變量的值,如下所示。

<p:column footerText="#{comp.getTotalPol()}"/> 
+0

您的變量未聲明爲「靜態」。 – mabi

+0

@mabi是的。我正在使用這個非靜態變量來訪問靜態變量,即OP在頁面上需要的值。如果你檢查我的getter方法,我正在訪問OP的靜態變量。 – Adarsh

+0

仍然沒有載入數據 – Sumit

4
public static int getPolTotal() { 
     return polTotal; 
    } 

你不能有一個getter靜態。 Primefaces視圖不會識別此。

更改getter爲:

public int getPolTotal() { 
     return polTotal; 
    } 

你仍然可以有variablestatic。 (private static int polTotal;