从primefaces datatable到托管bean获取复选框选择的值

时间:2013-06-30 08:36:01

标签: jsf-2 primefaces datatable

我正在尝试从primefaces datatable获取复选框选中的值 selectedCars 到名为 TableBean 托管bean 。我得到空指针异常在我试图获取函数 getSelection()中的值时,请帮我解决这个问题

我的JSF页面:

<h:form id="form">
  <p:dataTable id="multiCars" var="car"
    value="#{tableBean.mediumCarsModel}" paginator="true" rows="10"
    selection="#{tableBean.selectedCars}">

    <f:facet name="header">  
      Checkbox Based Selection  
    </f:facet>

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

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

    <p:column headerText="Year" style="width:25%">  
      #{car.year}  
    </p:column>

    <p:column headerText="Manufacturer" style="width:24%">  
      #{car.manufacturer}  
    </p:column>

    <p:column headerText="Color" style="width:24%">  
      #{car.color}  
    </p:column>

    <f:facet name="footer">
      <p:commandButton id="multiViewButton" value="View"
        icon="ui-icon-search" update=":form:displayMulti"
        oncomplete="multiCarDialog.show()" />
    </f:facet>
  </p:dataTable>


  <p:dialog id="multiDialog" header="Car Detail"
    widgetVar="multiCarDialog" height="300" showEffect="fade"
    hideEffect="explode">

    <p:dataList id="displayMulti" value="#{tableBean.selectedCars}"
      var="selectedCar">  
      Model: #{selectedCar.model}, Year: #{selectedCar.year}  
   </p:dataList>

  </p:dialog>
</h:form>

我的托管bean

@SessionScoped
@ManagedBean
public class TableBean implements Serializable {

  private List<Car> cars;

  private Car[] selectedCars;

  private CarDataModel mediumCarsModel;
  Connection connection;
  Statement stmt;
  ResultSet rs;

  public TableBean() {
    cars = new ArrayList<Car>();
    getCars();
    getSelection();
    mediumCarsModel = new CarDataModel(cars);
  }

  public Car[] getSelectedCars() {
      return selectedCars;
  }

  public void setSelectedCars(Car[] selectedCars) {
      this.selectedCars = selectedCars;
  }


  public void getCars() {
      int i = 0;
      try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:jtds:sqlserver://cvgapp106I/dev2_LPSR");
        System.out.println("connected to the database");

        stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("select * from test");

        while(rs.next()) {
        cars.add(i,new Car(rs.getString("Model"),rs.getInt("Year"),rs.getString("Manufacturer"),rs.getString("Color")));
            i++;
        }

      }  catch (Exception e) {
      e.printStackTrace();
      }
  }

  public void getSelection() {
    System.out.println(selectedCars[0].getModel()); 
  }



  public CarDataModel getMediumCarsModel() {
    return mediumCarsModel;
  }

  public void setMediumCarsModel(CarDataModel mediumCarsModel) {
    this.mediumCarsModel = mediumCarsModel;
  }
}

2 个答案:

答案 0 :(得分:0)

  public void getSelection() {
    System.out.println(selectedCars[0].getModel()); 
  }

如果列表为空,这将抛出NullPointerException

如果你真的想要使用它,那么你应该尝试检查列表是否为空

if(selectedCars!=null && !selectedCars.isEmpty()){
    System.out.println(selectedCars[0].getModel()); 

}

答案 1 :(得分:0)

不要在构造函数中调用getSelection()。 selectedCars将始终为null