2013-03-28 64 views
0

我在我的應用程序中遇到複選框選擇問題。我在一個頁面上有一個dataTable(index.xhtml)。在同一頁面上有一個ajax按鈕,當用戶點擊它時,應用程序應該導航到另一個頁面(detail.xhtml)。詳細信息頁面包含用於導航回index.xhtml的返回按鈕。導航工作正常,但當用戶從詳細信息頁返回時,當用戶點擊它時,數據表中的行復選框不會被選中(選中所有行的標題複選框有效)。當我重複場景(又名訪問詳細信息頁面並返回)時,複選框再次工作。第三次重複後,他們不再工作(所以每秒鐘導航他們不工作)。當我在按鈕上使用ajax =「false」或faces-redirect = true時,一切正常。Primefaces:ajax導航和dataTable複選框選擇

使用鑽嘴魚科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

你們中許多人需要從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"; 
} 
+0

正如我在我的文章中所述,faces-redirect工程,但我不想使用它 – user2219927