Primefaces:ajax导航和dataTable复选框选择

时间:2013-03-28 14:34:49

标签: java ajax jsf primefaces

我在申请中遇到了复选框选择问题。我在一个页面上有一个dataTable(index.xhtml)。在同一页面上有一个ajax按钮,当用户点击它时,应用程序应该导航到另一个页面(detail.xhtml)。详细信息页面包含用于导航回index.xhtml的返回按钮。导航工作但当用户从详细信息页面返回时,dataTable中的行复选框在用户单击时不会被选中(标题复选框选择所有行都有效)。当我重复场景(又名访问详细信息页面并返回)时,复选框再次正常工作。在第三次重复之后,它们不再工作(所以每次导航它们都不起作用)。当我使用ajax =" false"或按钮上的faces-redirect = true,一切正常。

使用Mojarra 2.10.19,PF 3.5和Glassfish 3.2.1

为简单起见,我通过简单的例子重新创建问题:

的index.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"      
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui" >

<h:head></h:head>   
<h:body>
    <h:form>
        <p:commandButton value="Add" action="add" />
        <p:dataTable id="cars" var="car" value="#{tableBean.mediumCarsModel}" 
                     selection="#{tableBean.selectedItems}" >

            <p:column selectionMode="multiple" style="width: 2%" />

            <p:column headerText="Model">
                #{car.model}
            </p:column>

        </p:dataTable> 
    </h:form>    
</h:body>

detail.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui" >

<h:head></h:head>

<h:body>
     <h:form>
      <p:commandButton value="Return" action="return" /> 
     </h:form>    
</h:body>

TableBean.java

@ManagedBean
@SessionScoped
public class TableBean {    
    private final static String[] manufacturers;

    static { 
      manufacturers = new String[10];
      manufacturers[0] = "Mercedes";
      manufacturers[1] = "BMW";
      manufacturers[2] = "Volvo";
      manufacturers[3] = "Audi";
      manufacturers[4] = "Renault";
      manufacturers[5] = "Opel";
      manufacturers[6] = "Volkswagen";
      manufacturers[7] = "Chrysler";
      manufacturers[8] = "Ferrari";
      manufacturers[9] = "Ford";
    }

    private List<Car> carsSmall;    
    private CarDataModel mediumCarsModel;
    private List<Car> selectedItems; 

    public TableBean() {
        carsSmall = new ArrayList<Car>();
        populateRandomCars(carsSmall, 5);
        mediumCarsModel = new CarDataModel(carsSmall);
    }

    private void populateRandomCars(List<Car> list, int size) {
        for (int i = 0; i < size; i++) {
            list.add(new Car(manufacturers[i]));
        }
    }

    public List<Car> getSelectedItems() {
        return selectedItems;
    }

    public void setSelectedItems(List<Car> selectedItems) {        
        this.selectedItems = selectedItems;
    }

    public CarDataModel getMediumCarsModel() {        
        return mediumCarsModel;
    }
}

CarDataModel.java

public class CarDataModel extends ListDataModel<Car> implements SelectableDataModel<Car> {  

    public CarDataModel(List<Car> data) {
        super(data);
    }

    @Override
    public Car getRowData(String rowKey) { 
        List<Car> cars = (List<Car>) getWrappedData();

        for(Car car : cars) {
            if(car.getModel().equals(rowKey)){                
                return car;
            }
        }
        return null;
    }

    @Override
    public Object getRowKey(Car car) {
        return car.getModel();
    }
}

Car.java

public class Car implements Serializable {

    private String model;

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public Car(String model) {
        this.model = model;
    }
}

faces-config.xml中

<navigation-rule>
    <from-view-id>/index.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>add</from-outcome>
        <to-view-id>/detail.xhtml</to-view-id>
    </navigation-case>       
</navigation-rule>   

<navigation-rule>
    <from-view-id>/detail.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>return</from-outcome>
        <to-view-id>/index.xhtml</to-view-id>
    </navigation-case>       
</navigation-rule> 

1 个答案:

答案 0 :(得分:1)

您需要从faces-config.xml中删除导航规则以尝试以下操作...

<强>的index.xhtml

<p:commandButton value="Add" action="#{tableBean.redirectToDetail}" />

<强> detail.xhtml

<p:commandButton value="Return" action="#{tableBean.redirectToIndex}" />

<强> TableBean.java

@ManagedBean(name = "tableBean")
...
...
...
public String redirectToDetail() {
    return "detail?faces-redirect=true";
}
public String redirectToIndex() {
    return "index?faces-redirect=true";
}