【问题标题】:cant display static int variable in primefaces datatable无法在 primefaces 数据表中显示静态 int 变量
【发布时间】:2014-02-20 10:10:22
【问题描述】:

我使用 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;
    }
}

【问题讨论】:

  • 你的 polTotal 的吸气剂在哪里??
  • 在上面的同一个 bean 中我只是没有再次显示检查我编辑了上面的 bean
  • 发布您的完整课程。
  • 刚刚上传的课程现在可以查看了
  • 需要注意的一点:你的构造函数是多余的,你可以去掉它。另外,您需要知道 setter 的调用次数可能比您预期的要多。在其中做任何类型的额外工作都是一个非常糟糕的主意。我们可以假设excelReport.report6List&lt;Excel_Target_Compliance&gt; 吗?

标签: java jsf jsf-2 primefaces


【解决方案1】:
  public static int getPolTotal() {
        return polTotal;
    }

您不能将getter 设为静态。Primefaces 视图无法识别此内容。

getter 更改为:

 public int getPolTotal() {
        return polTotal;
    }

您仍然可以将variable 设置为static。 (private static int polTotal;)

【讨论】:

    【解决方案2】:

    其他答案已经提到,您不能以这种方式使用 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 代码中,您必须为 p:dataTable 引用 lines 元素:

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

    【讨论】:

    【解决方案3】:

    据我了解,静态变量不属于实例,而是属于包含该变量的类。我认为您不能直接从 JSF 访问静态变量。相反,您可以使用其 getter 方法创建另一个变量来获取静态变量的值,如下所示。

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

    并在JSF页面上使用此方法访问静态变量的值,如下所示。

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

    【讨论】:

    • 您的变量未声明static
    • @mabi 是的。我正在使用这个非静态变量来访问静态变量,OP 在页面上需要它的值。如果您检查我的 getter 方法,我正在那里访问 OP 的静态变量。
    • 仍然没有加载数据
    • 如果这是唯一的目的,你不需要一个支持字段来拥有一个 getter 方法。
    • 没有错误@Adarsh,问题已经解决,感谢大家。
    猜你喜欢
    • 2013-09-22
    • 2022-09-28
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    相关资源
    最近更新 更多