我正在尝试学习PF,所以我首先显示数据表,然后在rowClick传递参数的页面上导航到下一页,但由于出现以下错误而陷入困境。对于该问题,我发现了类似的问题,但还没有运气。我希望有人能帮助我。

我收到以下错误:

DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled.

Caused by:
javax.faces.FacesException - DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled.

我的页面:
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">

<h:head>
<title>Primefaces 3.1</title>

</h:head>
<h:body>
    <h:form id="form">
            <p:dataTable value="#{tableBean.cars}" var="var" rowkey="#{var.model}"
            selection="#{tableBean.car}" selectionMode="single">
            <p:column>  <f:facet name="header">
                                <h:outputText styleClass="outputText" value="Model"></h:outputText>
                            </f:facet>
                            <h:outputText styleClass="outputText"
                                value="#{var.model}"></h:outputText>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText styleClass="outputText" value="Color"></h:outputText>
                            </f:facet>
                            <h:outputText styleClass="outputText"
                                value="#{var.randomColor}"></h:outputText>
                        </p:column></p:dataTable>
        </h:form>
</h:body>
</html>

我的 bean 类:
@ManagedBean
@ViewScoped
public class TableBean extends ListDataModel<Car> implements SelectableDataModel<Car>{

    private List<Car> cars;
    private Car car;

    public List<Car> getCars() {

        cars = new ArrayList<Car>();
        Car car1 = new Car();
        car1.setModel("BMW");
        car1.setRandomColor("Black");
        cars.add(car1);
        Car car2 = new Car();
        car2.setModel("Audi");
        car2.setRandomColor("White");
        cars.add(car2);

        return cars;
    }

    public String onRowSelect(){
        System.out.println("Row Click!!!");
        return "otherpage";//Does this nav works???if not how???
    }

    public Car getCar() {
        return car;
    }

    @Override
    public Car getRowData(String pArg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object getRowKey(Car pArg0) {
        // TODO Auto-generated method stub
        return null;
    }
    }

OtherBean:
public class Car{

    private String model;
    private String randomColor;

    public String getRandomColor() {
        return randomColor;
    }
    public void setRandomColor(String pRandomColor) {
        randomColor = pRandomColor;
    }

    public String getModel() {
        return model;
    }
    public void setModel(String pModel) {
        model = pModel;
    }

}

最佳答案

如果未在#{tableBean.cars}上指定SelectableDataModel属性,则rowKey必须实现<p:dataTable>

但是,您已经在#{tableBean}上实现了它。这个不对。您可以在PrimeFaces showcase中找到正确的示例。基本上,根据展示代码示例,您的TableBean类必须看起来像这样:

@ManagedBean
@ViewScoped
public class TableBean implements Serializable {

    private List<Car> cars;
    private Car car;
    private CarDataModel carsModel;

    public TableBean() {
        cars = new ArrayList<Car>();
        // Populate cars here and thus NOT in the getter method!
        carsModel = new CarDataModel(cars);
    }

    // ...
}
CarDataModel如下所示(再次从PrimeFaces展示代码示例中复制):
public class CarDataModel extends ListDataModel<Car> implements SelectableDataModel<Car> {

    public CarDataModel() {
    }

    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();
    }

}

最后,使用#{tableBean.carsModel}代替#{tableBean.cars}作为<p:dataTable>的值。完全与展示示例相同。
<p:dataTable value="#{tableBean.carsModel}" var="car" ... />

另一种更简单的方法是只在rowKey上指定<p:dataTable>属性。
<p:dataTable value="#{tableBean.cars}" var="car" rowKey="#{car.model}" ... />

这样,您不需要整个SelectableDataModel。您只需要确保它永远不是null,并且在各行中始终是唯一的。另请参见DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled, but I have already defined rowKey

10-06 14:35