假设我必须在数据表中呈现多个复选框,如下所示:
<?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>
所有复选框中的值属性都相同 - #{examBean.selectedCategoryList}
。
selectedCategoryList
在我的bean中声明为List<String>
属性,如下所示:
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);
}
}
我遇到的问题是,一旦提交表单,我只会看到bean属性selectedCategoryList
中最后一个复选框的值。有人可以告诉我如何从所有复选框中收集值?我被迫为每个复选框提供不同的ID,因为我希望能够在单击按钮时单独折叠它们中的每一个。
我尝试将bean属性selectedCategoryList
更改为List<List<String>>
。然后在我的xhtml页面上,我尝试将复选框的value属性设置为
<h:selectManyCheckbox id="checkbox_#{status.index}" value="#{examBean.selectedCategoryList[status.index]}" style="display:none">
它没有真正起作用,我得到一个数组超出范围的异常为0.该列表已初始化,所以不确定我是如何获得一个超出范围的数组异常。
答案 0 :(得分:0)
我找到了一个解决方法。我将选择多个复选框更改为如下所示:
<h:selectManyCheckbox id="checkbox" value="#{cat.selectedCategories}" style="display:none">
然后,我将属性selectedCategories
添加到子对象,而不是寻找某种方式来获取父级别的选定项目。