2017-07-07 42 views
0

我在JSF應用程序中使用PrimeFaces 5.1。在此應用程序的列表頁面上,我將Primefaces Datatable與分頁結合使用。表中某列的值是一個primefaces命令鏈接,它在click上被定義爲調用具有SessionScope的JSF ManagedBean類中定義的動作方法。該數據表本身是在使用具有h:form定義的模板XHTML的XHTML頁面中定義的。因此這個數據表嵌套在那個h:表格中。p:commandLink在p:datatable中不起作用,頁面1後分頁

我面臨的問題是ManagedBean中定義的方法只能從datatable的第一頁上呈現的commandLinks中調用,而從後續頁面上的commandLinks不會調用action方法。我還注意到,由於總記錄數少於100,所以當我將XHTML中數據表的'rows'屬性的值設置爲每頁100行時,只有一個頁面被渲染,然後當我選擇10或50列表頁面中分頁下拉列表中的每頁行數選項顯示,但鏈接正常工作。但是,從代碼中,如果我在XHTML代碼中將行值設置爲每頁50或10行,問題似乎就會發生。

我檢查螢火JS以及鍍鉻JS控制檯,並沒有錯誤也沒有tomcat的日誌文件顯示,即使我在web.xml設置了javax.faces.PROJECT_STAGE的context-param到Development任何日誌錯誤。這種行爲的原因是什麼?我該如何解決這個問題?

下面是我對數據表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"); 
} 
} 
+0

2 sugestions ...創建一個[mcve]並嘗試最新的PF版本和最近的jsf版本 – Kukeltje

+0

可能以相反的順序完成 – Kukeltje

回答

0

根據需要,我終於得到了與分頁數據表的工作。 Primefaces數據表中有兩個屬性,分別是rowsfirst,我使用它們來使鏈接正常工作。我在我的Backing Bean類CustomerServicePortaltBacking中定義了兩個新屬性rowsfirst以及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屬性在數據表中以指向在輔助bean定義的row屬性如下

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

這使得數據表格可以很好地與分頁工作。