0

我需要一个带有 RadioCheckbox 的 DataTable 然后找到了链接

FacesException:启用选择时,DataModel 必须实现 org.primefaces.model.SelectableDataModel

所有我需要的,但是在运行页面时是空白的并且没有给出异常。

无论如何,我也会发布我的代码。

我的xhtml:

<?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" paginator="true" rows="10"
    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>

豆:

@ManagedBean
@SessionScoped
public class TableBean implements Serializable{

    private static final long serialVersionUID = 1L;

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

    public TableBean() {
        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);       

        carsModel = new CarDataModel(cars);
    }

    public List<Car> getCars() {   
        return cars;
    }

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

    public Car getCar() {
        return car;
    }    

    public CarDataModel getCarsModel() {
    return carsModel;
    }


    public void setCarsModel(CarDataModel carsModel) {
    this.carsModel = carsModel;
    }

汽车数据模型:

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

    private static final long serialVersionUID = 1L;

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

    @Override
    public Car getRowData(String rowKey) {
        @SuppressWarnings("unchecked")
        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();
    }

我不知道可以是什么。

谢谢!

4

1 回答 1

0

我认为问题出在这里:

public TableBean() {
        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);       

        carsModel = new CarDataModel(cars);
    }

您不能将值放入构造函数中的列表中。如果您将该代码移至该getCars()方法,它应该可以工作

public TableBean() {}

public List<Car> getCars() {   
    if(cars != null && !cars.isEmpty()) return cars;
    else {
                  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);       

            carsModel = new CarDataModel(cars);
    }
    return cars;
}
于 2012-10-09T11:34:23.480 回答