1

im using primefaces datatable to display my data here is my code:

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

Every thing is working fine except i'm not getting any data in my footer row. i'm trying to display the static variable of int type in my footerText.

here is by bean :

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;
    }
}
Sumit
  • 71
  • 7
  • where is your getter for polTotal?? – Makky Feb 20 '14 at 10:17
  • in same bean above i just didnt displayed check againg i edited the bean above – Sumit Feb 20 '14 at 10:21
  • post your full class . – Makky Feb 20 '14 at 10:24
  • just uploaded the class now u can check – Sumit Feb 20 '14 at 10:34
  • Some things to note: your constructor is redundant, you can remove it. Plus you need to know that setters might be called more times than you expect. Doing any kind of additional work in them is a really bad idea. Can we assume that `excelReport.report6` is a `List`? – mabi Feb 20 '14 at 10:50
  • i just created private variable in excelReport bean and in footerText i used excelReport.polTotal and in excelReport i just added setPolTotal(Excel_Compliance_Report.polTotal); – Sumit Feb 20 '14 at 11:26
  • Thanks to @LaurentG for his idea – Sumit Feb 20 '14 at 11:30

3 Answers3

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

You cannot have a getter as static.Primefaces view won't recognise this.

Change the getter as :

 public int getPolTotal() {
        return polTotal;
    }

You still can have variable as static. (private static int polTotal;)

Makky
  • 17,117
  • 17
  • 63
  • 86
3

Other answers already mentioned that you cannot access a static field using an EL-expression this way.

But I think your real issue is about object-oriented concepts. I see no reason to use static in Excel_Target_Compliance. That is even bad practice. Your bean is view-scoped and thus you will have many instances (one for each view displayed) but static fields will be unique for the whole application. Consequently you will get concurrency problems as soon as two users are accessing the same page.

In your case, I would create two classes: one representing a line in the table, one representing the whole table and which can returns the total information. That could be something like this (to simplify):

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
}

In the JSF code, you have to reference the lines element for the p:dataTable:

<p:dataTable value="#{complianceTable.lines}" ...>
LaurentG
  • 11,128
  • 9
  • 51
  • 66
1

From what I understand, a static variable does not belong to the instance but to the class containing the variable. I do not think you can directly access the static variable from the JSF. Instead you can create another variable with its getter method to fetche you the value of the static variable as below.

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

and use this method on the JSF page to access the value of the static variable as below.

<p:column footerText="#{comp.getTotalPol()}"/>
Adarsh
  • 3,613
  • 2
  • 21
  • 37
  • Your variable isn't declared `static`. – mabi Feb 20 '14 at 10:39
  • @mabi yes. i am using this non-static variable to access the static variable, the value of which the OP needs on the page. If you check my getter method, i am accessing the OP's static variable there. – Adarsh Feb 20 '14 at 10:40
  • still not loading data – Sumit Feb 20 '14 at 10:40
  • You don't need a backing field just to have a getter method, if that's the only purpose. – mabi Feb 20 '14 at 10:45
  • throwing no error @Adarsh and the issue is solved now i marked the answer thanks to everyone. – Sumit Feb 20 '14 at 11:19