我有一个问题列表。每个问题都有一个子类别和主要类别,每个子类别都有一个主要类别。我想列出上面这样的问题。
--主要类别1(1是id)
----子猫1
------问题1
------问题2
----子猫2
------问题3
-主要类别2
----子猫3
------问题4
------问题5
----子猫4
------问题6
等等。
html 部分在这里;
<ui:repeat var="mainCatvar" value="#{dprEdit.mainCategories}">
<h:outputText value="#{mainCatvar.mainCatName}" styleClass="mainTitle" /><br></br>
<ui:repeat var="subCategory" value="#{mainCatvar.subCategories}">
<h:outputText value="#{subCategory.subCatName}" styleClass="title" />
<h:selectManyCheckbox value="#{dprEdit.selectedProblems}" >
<f:selectItems value="#{subCategory.problems}" var="problem"
itemValue="#{problem.problemName}" itemLabel="#{problem.problemName}" />
</h:selectManyCheckbox>
</ui:repeat>
</ui:repeat>
<p:commandButton action="#{dprCreate.save}" value="Save" update="form1" ajax="false" style="width:150px" />
这是我的后端代码;
public class MainCatDAO extends BaseDAO {
private final static String SELECT_ALL_MAIN_CATEGORIES ="SELECT * FROM TESTAE.MAINCATPF";
public MainCatDAO(Connection connection) {
super(connection);
}
private List<MainCat> getTable(String query, Object... values) throws ClassNotFoundException, SQLException {
List<MainCat> table = new ArrayList<MainCat>();
List<Map<String, Object>> data = this.getData(query, values);
for (int i = 0; i < data.size(); i++) {
ArrayList<SubCat> subList = new ArrayList<SubCat>();
// subList'i tekrardan tanımlama yani clear etmeme sebebim; maincat'a subList'i set etmeme rağmen,
//sonrasında subList'i clear edince maincat'ın subList'i de sıfırlanıyor.
//Bu yüzden subList'i her seferinde tekrar tanımlıyorum.
Map<String, Object> row = new HashMap<String, Object>();
MainCat maincat = new MainCat();
row = data.get(i);
maincat.setMainCatId(((BigDecimal) row.get("MAINCATID")).intValue());
maincat.setMainCatName(((String) row.get("MAINCATNAM")).trim());
String sorgu = "SELECT * FROM TESTAE.SUBCATPF WHERE MAINCATID='"+((BigDecimal) row.get("MAINCATID")).intValue()+"'";
List<Map<String, Object>> dataSub = this.getData(sorgu);
for (int a = 0; a < dataSub.size(); a++) {
ArrayList<Problem> problemList = new ArrayList<Problem>();
SubCat subcat = new SubCat();
row = dataSub.get(a);
if (row.get("SUBCATNAME") == null)
subcat.setSubCatName("");
else
subcat.setSubCatName((((String) row.get("SUBCATNAME")).trim()));
subList.add(subcat);
String sorgu2 = "SELECT * FROM TESTAE.PROBLEMPF WHERE SUBCATID='"+((BigDecimal) row.get("SUBCATID")).intValue()+"'";
List<Map<String, Object>> dataProb = this.getData(sorgu2);
for (int p = 0; p < dataProb.size(); p++) {
Problem problem = new Problem();
row = dataProb.get(p);
Integer problemId = ((BigDecimal) row.get("PROBLEMID")).intValue();
if (row.get("PRBLMNAME") == null)
problem.setProblemName("");
else
problem.setProblemName((((String) row.get("PRBLMNAME")).trim()));
problemList.add(problem);
}
subcat.setProblems(problemList);
}
maincat.setSubCategories(subList);
table.add(maincat);
}
return table;
}
public List<MainCat> getmainCategories () throws ClassNotFoundException, SQLException {
return getTable(SELECT_ALL_MAIN_CATEGORIES);
}}
Here is my save function;
@ManagedBean(name = "dprEdit")
@ViewScoped
public class DprEdit implements Serializable {
private List<String> selectedProblems = new ArrayList<String>();
private List<MainCat> mainCategories;
public DprEdit() throws ClassNotFoundException, SQLException {
mainCategories = new MainCatDAO(ConnectionManager.getConnection()).getmainCategories();}
public String save() throws ClassNotFoundException, SQLException, ParseException {
DprDAO dprDAO = new DprDAO(ConnectionManager.getConnection());
for (int i = 0; i < selectedProblems.size(); i++) {
dpr.setFormId(dpr.getFormId());
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date today = new Date();
formDate = formatter.parse(formatter.format(today));
dpr.setFormDate(formDate);
dpr.setFilledBy(username);
dpr.setProblemName(selectedProblems.get(i));
String problemName = selectedProblems.get(i);
ProblemDAO problemDAO = new ProblemDAO(ConnectionManager.getConnection());
Problem prob = problemDAO.getProblemId(problemName);
dpr.setProblemId(prob.getProblemId());
dprDAO.insertDetail(dpr);
}
public List<String> getSelectedProblems() {
return selectedProblems;
}
public void setSelectedProblems(List<String> selectedProblems) {
this.selectedProblems = selectedProblems;
}
public Integer getMaincatid() {
return maincatid;
}
public void setMaincatid(Integer maincatid) {
this.maincatid = maincatid;
}}
我的问题模型属性;
private Integer problemId;
private String problemName;
private String subCategory;
private String mainCategory;
我的子类别模型属性;
private Integer subCatId;
private String subCatName;
private List<Problem> problems;
我的主要类别模型属性;
private Integer mainCatId;
private String mainCatName;
private List<SubCat> subCategories;
所以,问题是我单击列表中的一些问题,然后单击保存按钮,我希望选定的问题列表充满了我单击的问题。但是,所选问题列表为空!
你有什么建议吗?
谢谢大家。
仔细观察<h:selectManyCheckbox value>
,特别是相关的 bean。
<ui:repeat value="#{dprEdit.mainCategories}" var="mainCatvar">
<ui:repeat value="#{mainCatvar.subCategories}" var="subCategory">
<h:selectManyCheckbox value="#{dprEdit.selectedProblems}">
<f:selectItems value="#{subCategory.problems}" />
基本上,您将所有复选框组的选定值绑定到同一个 Bean 属性。在处理表单提交过程中,每次迭代都会覆盖上一次迭代的复选框组提交的值。如果在 setSelectedProblems()
资源库上放置了调试断点,则会注意到这一点。这样,您最终会得到最后一个复选框组的提交值。鉴于"返回空"投诉,显然您在那里没有选择任何内容。
这绝对是错误的。<h:selectManyCheckbox value>
应该以某种方式与当前迭代的#{subCategory}
相关联,而不是直接与#{dprEdit}
管理的 Bean 相关联。一种方法是用#{subCategory}
作为键映射它:
<h:selectManyCheckbox value="#{dprEdit.selectedProblems[subCategory]}">
跟:
private Map<SubCat, String[]> selectedProblems = new HashMap<>();
请注意,您不能在此处使用 List<String>
,因为泛型类型信息在运行时会擦除,并且缺省模型值String[]
,否则无法指示 JSF/EL。如果要使用固定类型,可以考虑将selectedProblems
移动到SubCat
中,以便可以继续使用List<String>
:
<h:selectManyCheckbox value="#{subCategory.selectedProblems}">