简体   繁体   English

DataTable中的JSF selectBooleanCheckbox值始终为false

[英]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. 当我单击调用用于过滤选定项目的方法的按钮时,即使选中了其复选框,每个选定项目的参数均为false。 It's like the value of selectBoolean checkbox is allways false. 就像selectBoolean复选框的值始终为false一样。 I don't realize the problem. 我没有意识到问题。 Why won't it set values to true? 为什么不将值设置为true?

I have a JSF page with dataTable: 我有一个带dataTable的JSF页面:

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

please place the dataTable and commandButton in the same form, then try again. 请以相同的形式放置dataTable和commandButton,然后重试。 currently, it looks like your checkbox selections are not submitted, because the dataTable is not nested in the same form. 当前,您的复选框选择似乎未提交,因为dataTable没有嵌套在同一表格中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM