Java 将多个“选择多个”复选框中的值存储到单个对象

Java 将多个“选择多个”复选框中的值存储到单个对象,java,jsf-2,Java,Jsf 2,假设我必须在数据表中呈现多个复选框,如下所示: <?xml version="1.0" encoding="UTF-8"?> <!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"

假设我必须在数据表中呈现多个复选框,如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
<!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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jstl/core"
      >
    <h:head>
        <!-- <h:outputStylesheet library="css" name="table-style.css"  /> -->
    </h:head>

    <h:body>

        <h1 align="center">Generate Exam</h1>
        <hr/>
        <h:form id="form">
            <h:panelGrid columns="2" width="100%">
                <h:panelGroup layout="block" style="text-align:center">
                    <h3>Level</h3>
                    <h:selectOneMenu value="#{examBean.candidateLevel}">
                        <f:selectItem itemValue="SR" itemLabel="Senior Level" />
                        <f:selectItem itemValue="MD" itemLabel="Mid Level" />
                        <f:selectItem itemValue="JR" itemLabel="Junior Level" />
                    </h:selectOneMenu>
                </h:panelGroup>
                <h:panelGroup layout="block" style="text-align:center">
                    <h3>Candidate</h3>
                    <h:selectOneMenu value="#{examBean.selectedCandidateId}">
                        <f:selectItems value="#{examBean.candidateList}" var="candidate" itemLabel="#{candidate.name}" itemValue="#{candidate.id}"/>
                    </h:selectOneMenu>
                </h:panelGroup>
            </h:panelGrid>
            <hr/>
            <h:dataTable value="#{examBean.parentCategoryList}" var="cat" binding="#{table}">
                <h:column>
                    <h:outputText value="#{cat.name}"/>
                    <h:commandButton id="button" type="button" value="+" onclick="expand('#{table.rowIndex}');"/>
                    <br/>
                    <h:selectManyCheckbox id="checkbox" value="#{examBean.selectedCategoryList}" style="display:none">
                        <f:selectItems value="#{cat.subCategoryList}" var="sub" itemLabel="#{sub.name}" itemValue="#{sub.id}"/>
                    </h:selectManyCheckbox>
                </h:column>
            </h:dataTable>
            <hr/>
            <h:panelGrid columns="1" width="100%" layout="block" style="text-align:center">
                <h:panelGroup style="text-align:center">
                    <h:commandButton value="Generate Exam" action="#{examBean.generateExam()}" />
                    <h:commandButton value="Reset" type="reset" />
                </h:panelGroup>
            </h:panelGrid>
        </h:form>

        <script type="text/javascript">
            function expand(val) {
                var button = "form:j_idt18:" + val + ":button";
                var checkbox = "form:j_idt18:" + val + ":checkbox";
                var buttonElement = document.getElementById(button);
                var checkboxElement = document.getElementById(checkbox);
                if(checkboxElement.style.display == 'none') {
                    checkboxElement.style.display = 'block';
                    buttonElement.value = '-';
                } else {
                    checkboxElement.style.display = 'none';
                    buttonElement.value = '+';
                }
            }
        </script>   
    </h:body>
</html>
package com.gtp.iqp.presentation.managedBeans;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gtp.iqp.business.bo.Candidate;
import com.gtp.iqp.business.bo.Category;
import com.gtp.iqp.business.delegate.CandidateDelegate;
import com.gtp.iqp.business.delegate.CategoryDelegate;
import com.gtp.iqp.business.delegate.ExamDelegate;
import com.gtp.iqp.presentation.dto.CandidateSubCategoryList;

@SuppressWarnings("serial")
@Component
public class ExamBean extends BaseManagedBean {

    @Autowired
    private CategoryDelegate               categoryDelegate;
    @Autowired
    private CandidateDelegate              candidateDelegate;
    @Autowired
    private ExamDelegate                   examDelegate;
    private List<Category>                 parentCategoryList;
    private List<String>                   selectedCategoryList;
    private List<Candidate>                candidateList;
    private String                         selectedCandidateId;
    private String                         candidateLevel;

    public ExamDelegate getExamDelegate() {
        return examDelegate;
    }
    public void setExamDelegate(ExamDelegate examDelegate) {
        this.examDelegate = examDelegate;
    }
    public String getSelectedCandidateId() {
        return selectedCandidateId;
    }
    public void setSelectedCandidateId(String selectedCandidateId) {
        this.selectedCandidateId = selectedCandidateId;
    }
    public List<Candidate> getCandidateList() {
        this.candidateList = candidateDelegate.getAllCandidates();
        System.out.println("Cands: " + candidateList.size());
        return candidateList;
    }
    public void setCandidateList(List<Candidate> candidateList) {
        this.candidateList = candidateList;
    }
    public CandidateDelegate getCandidateDelegate() {
        return candidateDelegate;
    }
    public void setCandidateDelegate(CandidateDelegate candidateDelegate) {
        this.candidateDelegate = candidateDelegate;
    }
    public String getCandidateLevel() {
        return candidateLevel;
    }
    public void setCandidateLevel(String candidateLevel) {
        this.candidateLevel = candidateLevel;
    }
    public CategoryDelegate getCategoryDelegate() {
        return categoryDelegate;
    }
    public void setCategoryDelegate(CategoryDelegate categoryDelegate) {
        this.categoryDelegate = categoryDelegate;
    }
    public List<Category> getParentCategoryList() {
        selectedCategoryList = new ArrayList<String>();
        parentCategoryList = categoryDelegate.getParentCategories();
        return parentCategoryList;
    }
    public void setParentCategoryList(List<Category> parentCategoryList) {
        this.parentCategoryList = parentCategoryList;
    }
    public List<String> getSelectedCategoryList() {
        return selectedCategoryList;
    }
    public void setSelectedCategoryList(List<String> selectedCategoryList) {
        this.selectedCategoryList = selectedCategoryList;
    }
    public void generateExam() {
        System.out.println("Chosen categories:");
        for(String cat: selectedCategoryList) {
            System.out.println(cat);
        }
        System.out.println();
        System.out.println("Chosen level: " + candidateLevel);
        System.out.println();
        System.out.println("Chosen candidate: " + selectedCandidateId);
        //examDelegate.generateExam(selectedCategoryList, candidateLevel, selectedCandidateId);
    }
}
<h:selectManyCheckbox id="checkbox" value="#{cat.selectedCategories}" style="display:none">
我遇到的问题是,一旦提交表单,我只会看到bean属性
selectedCategoryList
中最后一个复选框中的值。有人能告诉我如何从所有复选框中收集值吗?我被迫为每个复选框指定不同的ID,因为我希望能够在单击按钮时分别折叠每个复选框。 我尝试将bean属性
selectedCategoryList
更改为
列表
。然后在我的xhtml页面上,我尝试将复选框的value属性设置为

<h:selectManyCheckbox id="checkbox_#{status.index}" value="#{examBean.selectedCategoryList[status.index]}" style="display:none">


它实际上不起作用,我得到了0的数组越界异常。列表已初始化,因此不确定如何获得数组越界异常。

我找到了解决方法。我将“选择多个”复选框更改为如下所示的复选框:

    <?xml version="1.0" encoding="UTF-8"?>
<!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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jstl/core"
      >
    <h:head>
        <!-- <h:outputStylesheet library="css" name="table-style.css"  /> -->
    </h:head>

    <h:body>

        <h1 align="center">Generate Exam</h1>
        <hr/>
        <h:form id="form">
            <h:panelGrid columns="2" width="100%">
                <h:panelGroup layout="block" style="text-align:center">
                    <h3>Level</h3>
                    <h:selectOneMenu value="#{examBean.candidateLevel}">
                        <f:selectItem itemValue="SR" itemLabel="Senior Level" />
                        <f:selectItem itemValue="MD" itemLabel="Mid Level" />
                        <f:selectItem itemValue="JR" itemLabel="Junior Level" />
                    </h:selectOneMenu>
                </h:panelGroup>
                <h:panelGroup layout="block" style="text-align:center">
                    <h3>Candidate</h3>
                    <h:selectOneMenu value="#{examBean.selectedCandidateId}">
                        <f:selectItems value="#{examBean.candidateList}" var="candidate" itemLabel="#{candidate.name}" itemValue="#{candidate.id}"/>
                    </h:selectOneMenu>
                </h:panelGroup>
            </h:panelGrid>
            <hr/>
            <h:dataTable value="#{examBean.parentCategoryList}" var="cat" binding="#{table}">
                <h:column>
                    <h:outputText value="#{cat.name}"/>
                    <h:commandButton id="button" type="button" value="+" onclick="expand('#{table.rowIndex}');"/>
                    <br/>
                    <h:selectManyCheckbox id="checkbox" value="#{examBean.selectedCategoryList}" style="display:none">
                        <f:selectItems value="#{cat.subCategoryList}" var="sub" itemLabel="#{sub.name}" itemValue="#{sub.id}"/>
                    </h:selectManyCheckbox>
                </h:column>
            </h:dataTable>
            <hr/>
            <h:panelGrid columns="1" width="100%" layout="block" style="text-align:center">
                <h:panelGroup style="text-align:center">
                    <h:commandButton value="Generate Exam" action="#{examBean.generateExam()}" />
                    <h:commandButton value="Reset" type="reset" />
                </h:panelGroup>
            </h:panelGrid>
        </h:form>

        <script type="text/javascript">
            function expand(val) {
                var button = "form:j_idt18:" + val + ":button";
                var checkbox = "form:j_idt18:" + val + ":checkbox";
                var buttonElement = document.getElementById(button);
                var checkboxElement = document.getElementById(checkbox);
                if(checkboxElement.style.display == 'none') {
                    checkboxElement.style.display = 'block';
                    buttonElement.value = '-';
                } else {
                    checkboxElement.style.display = 'none';
                    buttonElement.value = '+';
                }
            }
        </script>   
    </h:body>
</html>
package com.gtp.iqp.presentation.managedBeans;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gtp.iqp.business.bo.Candidate;
import com.gtp.iqp.business.bo.Category;
import com.gtp.iqp.business.delegate.CandidateDelegate;
import com.gtp.iqp.business.delegate.CategoryDelegate;
import com.gtp.iqp.business.delegate.ExamDelegate;
import com.gtp.iqp.presentation.dto.CandidateSubCategoryList;

@SuppressWarnings("serial")
@Component
public class ExamBean extends BaseManagedBean {

    @Autowired
    private CategoryDelegate               categoryDelegate;
    @Autowired
    private CandidateDelegate              candidateDelegate;
    @Autowired
    private ExamDelegate                   examDelegate;
    private List<Category>                 parentCategoryList;
    private List<String>                   selectedCategoryList;
    private List<Candidate>                candidateList;
    private String                         selectedCandidateId;
    private String                         candidateLevel;

    public ExamDelegate getExamDelegate() {
        return examDelegate;
    }
    public void setExamDelegate(ExamDelegate examDelegate) {
        this.examDelegate = examDelegate;
    }
    public String getSelectedCandidateId() {
        return selectedCandidateId;
    }
    public void setSelectedCandidateId(String selectedCandidateId) {
        this.selectedCandidateId = selectedCandidateId;
    }
    public List<Candidate> getCandidateList() {
        this.candidateList = candidateDelegate.getAllCandidates();
        System.out.println("Cands: " + candidateList.size());
        return candidateList;
    }
    public void setCandidateList(List<Candidate> candidateList) {
        this.candidateList = candidateList;
    }
    public CandidateDelegate getCandidateDelegate() {
        return candidateDelegate;
    }
    public void setCandidateDelegate(CandidateDelegate candidateDelegate) {
        this.candidateDelegate = candidateDelegate;
    }
    public String getCandidateLevel() {
        return candidateLevel;
    }
    public void setCandidateLevel(String candidateLevel) {
        this.candidateLevel = candidateLevel;
    }
    public CategoryDelegate getCategoryDelegate() {
        return categoryDelegate;
    }
    public void setCategoryDelegate(CategoryDelegate categoryDelegate) {
        this.categoryDelegate = categoryDelegate;
    }
    public List<Category> getParentCategoryList() {
        selectedCategoryList = new ArrayList<String>();
        parentCategoryList = categoryDelegate.getParentCategories();
        return parentCategoryList;
    }
    public void setParentCategoryList(List<Category> parentCategoryList) {
        this.parentCategoryList = parentCategoryList;
    }
    public List<String> getSelectedCategoryList() {
        return selectedCategoryList;
    }
    public void setSelectedCategoryList(List<String> selectedCategoryList) {
        this.selectedCategoryList = selectedCategoryList;
    }
    public void generateExam() {
        System.out.println("Chosen categories:");
        for(String cat: selectedCategoryList) {
            System.out.println(cat);
        }
        System.out.println();
        System.out.println("Chosen level: " + candidateLevel);
        System.out.println();
        System.out.println("Chosen candidate: " + selectedCandidateId);
        //examDelegate.generateExam(selectedCategoryList, candidateLevel, selectedCandidateId);
    }
}
<h:selectManyCheckbox id="checkbox" value="#{cat.selectedCategories}" style="display:none">


然后,我将property
selectedCategories
添加到子对象中,而不是寻找某种方法在父级获取所选项目。

在JSF中使用JSTL核心功能是不可取的,请参见示例。为什么不尝试将其编码为h:dataTable(它也为您进行id编号,因此不再存在唯一的id异常)…这仍然不能解决我的问题。我已将实现更改为数据表。请看我编辑的帖子。我试图找出如何从所有复选框中获取值,而不仅仅是最后一个复选框。