p:commandLink在第1页之后不在p:datatable中使用分页

时间:2017-07-07 18:07:36

标签: primefaces jsf-2

我在JSF应用程序中使用PrimeFaces 5.1。在这个应用程序的列表页面上,我正在使用Primefaces Datatable和分页。表中列之一的值是一个primefaces commandLink,它在click上定义为调用具有SessionScope的JSF ManagedBean类中定义的action方法。此数据表本身在XHTML页面中定义,该页面使用具有h:form定义的模板XHTML。因此,这个数据表嵌套在h:form。

我面临的问题是ManagedBean中定义的方法只能从数据表第一页上呈现的commandLinks调用,而后续页面上的commandLinks不会调用action方法。我还注意到,因为总记录小于100所以当我将XHTML中数据表的'rows'属性值设置为每页100行时,只有一个页面被渲染,然后当我选择10或50时列表页面中的分页下拉列表中的每页行数选项显示多个页面,但链接工作正常。但是,从代码中,如果我在XHTML代码中将行值设置为每页50行或10行,则问题似乎就会发生。

我检查了firebug JS以及Chrome JS控制台,即使我已将javax.faces.PROJECT_STAGE上下文参数web.xml设置为Development,也没有错误也没有出现任何日志错误{1}}。这种行为可能是什么原因,我该如何解决这个问题?

以下是我的数据表的JSF代码段:

<p:dataTable id="data" widgetVar="dataTable" var="customer" value="#{customerServicePortaltBacking.customers}"
         paginator="true"
         rows="50"
         paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
         rowsPerPageTemplate="10,50,100">
<p:column style="width: 14%">
    <f:facet name="header">
        <p:outputLabel value="#{msg['customerId']}" styleClass="covFont"/>
    </f:facet>
    <h:outputText value="#{customer.customerId}" styleClass="covFont"/>
</p:column>
<p:column style="width: 66%">
    <f:facet name="header">
        <p:outputLabel value="#{msg['name']}" styleClass="covFont"/>
    </f:facet>
    <h:outputText value="#{customer.name}" styleClass="covFont"/>
</p:column>
<p:column style="width: 10%">
    <f:facet name="header">
        <p:outputLabel value="#{msg['deptId']}" styleClass="covFont"/>
    </f:facet>
    <h:outputText value="#{customer.deptId}" styleClass="covFont"/>
</p:column>
<p:column style="width: 10%">
    <f:facet name="header">
        <p:outputLabel value="#{msg['view']}" styleClass="covFont"/>
    </f:facet>
    <p:commandLink id="invoiceButton" value="#{msg['customers.invoices']}" action="#{customerServicePortaltBacking.goInvoiceCategories(customer)}"
    ></p:commandLink>
</p:column>

</p:dataTable>

以下是Managed Bean类的代码:

package com.assetworks.csportal;

import com.assetworks.csportal.orm.*;
import org.apache.commons.lang.StringUtils;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.text.MessageFormat;
import java.util.*;
import java.util.logging.Logger;

@ManagedBean
@SessionScoped
public class CustomerServicePortaltBacking {

private static final Logger LOGGER = 
Logger.getLogger(CustomerServicePortaltBacking.class.getName());

private List<Customer> customers = null;
private Stack<Screens> screensStack = new Stack<>();
//Screen
private Screens screen = Screens.CustomerBrowse;

private String customerName;

private Customer customer;

public String getCustomerName() {
    if(customerName == null){

        String name = new DataAccess().getCustomerName(getContactId());
        if(name == null)
            name = "";
        customerName = name;
    }
    return customerName;
}

public void setCustomerName(String customerName) {
    this.customerName = customerName;
}

public List<Customer> getCustomers() {
    if(customers == null){
        customers = new DataAccess().getCustomers(getContactId());
    }
    List<Customer> filteredCustomers = new LinkedList<>();
    if(hasValue(this.getSearch())) {
        String search = getSearch().toUpperCase();
        for (Customer customer : customers) {
            if(customer.getCustomerId().indexOf(search) != -1 ||     customer.getName().toUpperCase().indexOf(search) != -1 || customer.getDeptId().indexOf(search) != -1){
                filteredCustomers.add(customer);
            }
        }
    }
    else{
        filteredCustomers = customers;
    }
    return filteredCustomers;
}

public String goCusomerList(){
    screensStack.clear();
    DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("form:data");
    if(dataTable != null) {
        dataTable.resetValue();
        dataTable.processUpdates(FacesContext.getCurrentInstance());
    }
    setScreen(Screens.CustomerBrowse);
    return Screens.CustomerBrowse.getId();
}

public String goHome(){
    search = null;
    return goCusomerList();
}

public String goInvoiceCategories(Customer customer){
    categories = null;
    this.customer = customer;
    this.setScreen(Screens.CustomerServiceCategory);
    return Screens.CustomerServiceCategory.getId();
}

public String goBack() {
    this.screen = screensStack.pop();
    return getScreen().getId();
}

public boolean hasValue(String str){
    return str != null && str.length() > 0;
}

public void setCustomer(Customer customer) {
    this.customer = customer;
}

public String authenticateUser() {
    isUserAuthenticated = true;
    return goCusomerList();
}

public void setScreen(Screens screen) {
    screensStack.push(getScreen());
    this.screen = screen;
}

public Screens getScreen() {
    return this.screen;
}

public String getMessage(String key, Object[] args){
    String result = "[Key " + key + " not found in messages.properties]";
    try {
        FacesContext context = FacesContext.getCurrentInstance();
        ResourceBundle bundle =
          ResourceBundle.getBundle("messages",
            context.getViewRoot().getLocale());
        result = new MessageFormat(bundle.getString(key)).format(args);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}


public String getVersion(){
    return Application.VERSION;
}

public String getScreenlabel() {
    return getMessage(screen.getId(), new Object[0]);
}

public Customer getCustomer() {
    return customer;
}

public void setCustomers(List<Customer> customers) {
    this.customers = customers;
}

public boolean isCustomerExternal(){
    return customer.getDeptId().toUpperCase().startsWith("UFL");
}
}

1 个答案:

答案 0 :(得分:0)

我终于让datatable按照需要使用分页。 Primefaces数据表上有两个属性,即rowsfirst,我与之合作以使链接正常工作。我在我的Backing Bean类rows中定义了两个新属性firstCustomerServicePortaltBacking以及下面给出的getter和setter。

    private Integer rows = 50;

    private Integer first = 0;

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public Integer getFirst() {
        return first;
    }

    public void setFirst(Integer first) {
        this.first = first;
    }

接下来,我将first属性添加到xhtml中定义的数据表中,并将其指向first属性并更新数据表中的row属性以指向row在后台bean中定义的属性如下

    <p:dataTable id="data" widgetVar="dataTable" var="customer" value="#{customerServicePortaltBacking.customers}"
                 paginator="true"
                 first="#{customerServicePortaltBacking.first}"
                 rows="#{customerServicePortaltBacking.rows}"
                 paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                 rowsPerPageTemplate="10,50,100,200,500" >

这使得数据表的分页工作正常。