带有RadioCheckbox的DataTable为空

时间:2012-10-09 11:06:36

标签: jsf primefaces

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

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

我需要的所有内容,但在运行页面时是空白的,不会例外。

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

我的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;
    }

CarDataModel:

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

我不知道会发生什么。

谢谢!

1 个答案:

答案 0 :(得分: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;
}