如何在JSF中选择dataTable中的多行?

  • 本文关键字:dataTable 选择 JSF jsf
  • 更新时间 :
  • 英文 :


我在选择h:dataTable的多行时遇到了一些问题。我的代码如下:

<h:dataTable value="#{reportBean.lstchalan}"  var="chalan" >
 <h:column  >
      <f:facet name="header">
            <h:outputText value="Select" />
        </f:facet>
         <h:selectBooleanCheckbox value="#{reportBean.checked[chalan.issueNo]}" />
</h:column>  
...
</h:dataTable>
<h:commandButton value="submit" action="#{reportBean.submit()}" />    

和下面是我的后台bean:

  public class ReportBean {
  List<ChalanVo> checkedItems = new ArrayList<ChalanVo>();
  private Map<String, Boolean> checked = new HashMap<String, Boolean>();
  ........
  public List<ChalanVo> getCheckedItems() {
    return checkedItems;
  }
  public void setCheckedItems(List<ChalanVo> checkedItems) {
    this.checkedItems = checkedItems;
 }
 public Map<String, Boolean> getChecked() {
    return checked;
 }
 public void setChecked(Map<String, Boolean> checked) {
    this.checked = checked;
 }
 public String submit() {

checkedItems = new ArrayList<ChalanVo>();
    for (ChalanVo dataItem : lstchalan) {
        if (checked.get(dataItem.getIssueNo())) {
            checkedItems.add(dataItem);
            checked.remove(dataItem.getIssueNo()); 
        }
    }}
}

但是当执行(ChalanVo dataItem: lstchalan)时,我得到了一个异常。Lstchalan给出null。你能帮我理解我哪里做错了吗?

下面是一个简单的例子:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
@Named("test")
@ViewScoped
public class TestBean implements Serializable{
    private static final long serialVersionUID = -1064219566884774973L;
    private List<ChalanVo> lstChalans;
    private Map<ChalanVo, Boolean> checkedItems = new HashMap<TestBean.ChalanVo, Boolean>();
    public TestBean() {
        lstChalans = new ArrayList<TestBean.ChalanVo>();
        lstChalans.add(new ChalanVo("test1"));
        lstChalans.add(new ChalanVo("test2"));
        lstChalans.add(new ChalanVo("test3"));
    }
    public List<ChalanVo> getLstChalans() {
        return lstChalans;
    }
    public void setLstChalans(List<ChalanVo> lstChalans) {
        this.lstChalans = lstChalans;
    }
    public Map<ChalanVo, Boolean>  getCheckedItems() {
        return checkedItems;
    }
    public void setCheckedItems(Map<ChalanVo, Boolean>  checkedItems) {
        this.checkedItems = checkedItems;
    }
    public void save() {
        System.out.println("save");
        for (Entry<ChalanVo, Boolean> e : checkedItems.entrySet()) {
            if (e.getValue()) {
                System.out.println("checked: " + e.getKey().getIssueNo());
            }
        }
    }
    public class ChalanVo {
        private String issueNo;
        public ChalanVo(String issueNo) {
            setIssueNo(issueNo);
        }
        public String getIssueNo() {
            return issueNo;
        }
        public void setIssueNo(String issueNo) {
            this.issueNo = issueNo;
        }
    }
}

使用这个xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head />
<h:body>
        <h:form>
            <h:dataTable value="#{test.lstChalans}" var="chalan">
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Select" />
                    </f:facet>
                    <h:selectBooleanCheckbox value="#{test.checkedItems[chalan]}"  />
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Issue No" />
                    </f:facet>
                    <h:outputText value="#{chalan.issueNo}"/>
                </h:column>
            </h:dataTable>
            <h:commandButton action="#{test.save()}" value="Submit" />
        </h:form>
</h:body>
</html>

save()方法能够列出所选项目。我认为NullPointerException与复选框无关。无论如何,你可以这样选择

相关内容

  • 没有找到相关文章

最新更新