简体   繁体   English

p:commandLink在第1页后的分页中在p:datatable中不起作用

[英]p:commandLink not working in p:datatable with pagination after page 1

I am using PrimeFaces 5.1 in my JSF application. 我在JSF应用程序中使用PrimeFaces 5.1。 On the listing page of this application I am using Primefaces Datatable with pagination. 在此应用程序的列表页面上,我正在使用带有分页功能的Primefaces Datatable。 Values for one of the column in the table is a primefaces commandLink that on click is defined to call action method defined in a JSF ManagedBean class that has SessionScope. 表中该列之一的值是primefaces commandLink,单击该链接时将其定义为调用具有SessionScope的JSF ManagedBean类中定义的操作方法。 This datatable itself is defined in an XHTML page that uses template XHTML that has h:form defined. 该数据表本身是在使用定义了h:form的模板XHTML的XHTML页面中定义的。 Thus this datatable is nested in that h:form. 因此,此数据表嵌套在该h:form中。

The problem I am facing is that the method defined in ManagedBean is only getting invoked from commandLinks rendered on the first page of the datatable, whereas from commandLinks on subsequent pages action method is not getting invoked. 我面临的问题是,只能从在数据表的第一页上呈现的commandLinks调用ManagedBean中定义的方法,而不能从后续页面上的commandLinks调用action方法。 What I also noticed is that as the total records are less than 100 so when I set the value for 'rows' attribute of the datatable in XHTML to 100 rows per page, only a single page gets rendered and then when I select 10 or 50 rows per page option from the pagination drop down list in the listing page multiple pages show up but the link works fine. 我还注意到,由于总记录少于100条,因此当我将XHTML中数据表的“行”属性的值设置为每页100行时,仅呈现一个页面,然后选择10或50列表页面中的分页下拉列表中的“每页行数”选项可以显示多个页面,但链接可以正常工作。 However, from the code, if I set the rows value to 50 or 10 rows per page in the XHTML code the issue seems to happen. 但是,从代码中,如果我在XHTML代码中将行值设置为每页50或10行,则似乎会发生此问题。

I checked firebug JS as well as Chrome JS console and there are no errors nor does the tomcat log file shows any log error even though I have set the javax.faces.PROJECT_STAGE context-param in web.xml to Development . 我检查了firebug JS以及Chrome JS控制台,即使我将web.xmljavax.faces.PROJECT_STAGE context-param设置为Development ,也没有错误,tomcat日志文件也没有显示任何日志错误。 What could be the reason for this behavior and how can I resolve this? 发生这种现象的原因可能是什么,我该如何解决?

Below is my JSF snippet for the datatable: 以下是我的数据表的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>

Below is the code from Managed Bean class: 以下是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");
}
}

I finally got datatable working with pagination as desired. 我终于得到了所需的分页数据表。 There are two attributes on Primefaces datatable namely rows and first that I worked with to get the links working. Primefaces数据表上有两个属性,即rows ,而我first使用它来使链接正常工作。 I defined two new properties rows and first in my Backing Bean class CustomerServicePortaltBacking along with getter and setter as given below. 我定义了两个新的属性rows并且在Backing Bean类CustomerServicePortaltBacking first定义了这两个属性rows ,如下所示,还有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;
    }

Next, I added first attribute to the datatable defined in xhtml and pointed it to first property and updated the row attribute in the datatable to point to the row property defined in the backing bean as below 接下来,我将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" >

This got the datatable working fine with pagination. 这样可以使数据表在分页时正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM