jsfdatatableselectbooleancheckbox

JSF selectBooleanCheckbox value in DataTable is allways false


When i click on the button that invokes the method for filtering selected items, every items selected parameter is false even though the checkbox for it is checked. It's like the value of selectBoolean checkbox is allways false. I don't realize the problem. Why won't it set values to true?

I have a JSF page with dataTable:

<h:dataTable value="#{productManagedBean.showProducts()}" var="item">
   <h:column>
      <f:facet name="header">
         <h:outputText value="Select"/>
      </f:facet>
      <h:selectBooleanCheckbox value="#{item.selected}"></h:selectBooleanCheckbox>
   </h:column>
<h:column>
   <f:facet name="header">
      <h:outputText value="Image"/>
   </f:facet>
   <div class="container-images-product-list">
      <h:graphicImage value='#{item.product.image}' class="images-product-list" />
   </div>

<h:form>
   <h:commandButton value="Delete selected product/s" class="pure-button pure-button-primary" action="#{productManagedBean.filterCheckedItems()}"></h:commandButton>
</h:form>

Class of Items contained in dataTable:

public class ProductSelection {
    private Product product;
    private boolean selected;

    public ProductSelection(Product product) {
        this.product = product;
    }

    public ProductSelection(Product product, boolean selected) {
        this.product = product;
        this.selected = selected;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

Methods in ManagedBean:

public List<ProductSelection> showProducts(){
    if(itemsShown == false) {
        list = productSessionBean.getAllProducts();
        for(Product p: list){
            selectionList.add(new ProductSelection(p));
        }
        itemsShown = true;
    }
    for (ProductSelection p: selectionList) {
        p.getProduct().setImage("../uploaded/" + p.getProduct().getImage());
    }
    return selectionList;   
}

public void filterCheckedItems(){
    for (ProductSelection p: selectionList) {
        if(p.isSelected()){
            checkedList.add(p.getProduct());
        }
    }
}

Solution

  • please place the dataTable and commandButton in the same form, then try again. currently, it looks like your checkbox selections are not submitted, because the dataTable is not nested in the same form.