我正在使用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类型的静态变量。
这里是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;
}
}发布于 2014-02-20 10:58:10
其他答案已经提到,您不能以这种方式使用EL表达式访问静态字段。
但我认为你真正的问题是面向对象的概念。我认为没有理由在static中使用Excel_Target_Compliance。这甚至是糟糕的做法。您的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代码中,必须引用p:dataTable的p:dataTable元素
<p:dataTable value="#{complianceTable.lines}" ...>发布于 2014-02-20 10:37:09
public static int getPolTotal() {
return polTotal;
}您不能将getter作为静态的。Primefaces视图不会识别这一点。
将getter更改为:
public int getPolTotal() {
return polTotal;
}您仍然可以使用variable作为static。(private static int polTotal;)
发布于 2014-02-20 10:33:18
据我所知,静态变量不属于实例,而是属于包含变量的类。我认为您不能直接访问JSF中的静态变量。相反,您可以使用其getter方法创建另一个变量,以获取静态变量的值,如下所示。
public int getTotalPol(){
return Bean.polTotal;
}并在JSF页面上使用此方法访问静态变量的值,如下所示。
<p:column footerText="#{comp.getTotalPol()}"/>https://stackoverflow.com/questions/21904544
复制相似问题