0

我有一个问题清单。每个问题都有一个子类别和一个主类别,每个子类别也有一个主类别。我想列出上述问题。

--Main category1 (1是id)
----subcat1
------problem1
------ problem2
----subcat2
------problem3

--main category2
----subcat3
------problem4
------problem5
----subcat4
------problem6

等等。

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;

所以,问题是我单击列表上的一些问题,然后单击保存按钮,我希望该 selectedProblem 列表充满了我单击的问题。但是 selectedProblems 列表是空的!

你有什么建议吗?

谢谢你们。

4

1 回答 1

0

仔细查看<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()器上放置了一个调试断点,您会注意到这一点。这样,您最终会得到最后一个复选框组的提交值。鉴于“return null”投诉,显然您在那里没有选择任何内容。

这绝对是错误的。<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}">
于 2014-12-31T11:42:38.080 回答