JSF h:ui中的selectManyCheckbox:通过Map条目重复

时间:2012-11-25 09:33:45

标签: java jsf jsf-2 map

我遇到问题,当我按String将值List<String>放入<ui:repeat>内{按钮}时我看到List<String>只包含ui最后一个元素的记录:重复

<ui:repeat value="#{util:toList(test.mapQestionsWithAnswers)}" var="entry">
                <h:inputHidden value="#{entry.key.questionId}" />
                <h:outputText value="#{entry.key.question}"
                    rendered="#{not empty entry.value}" />
                <p:selectManyCheckbox value="#{test.questionAnswerList}"
                    layout="pageDirection">
                    <f:selectItems value="#{entry.value}" var="ans"
                        itemValue="#{entry.key.questionId} #{ans.answer.answerId}"
                        itemLabel="${ans.answer.answer}" />
                </p:selectManyCheckbox>
            </ui:repeat>

来自Baluscanswer How to show hashmap values in jsf?util:toList的想法

//修订版 我在不使用map

的情况下重写了这段代码
<ui:repeat value="${test.questionList}" var="question">
                <h:outputText value="#{question.question}"
                    rendered="#{not empty question}" />
                <p:selectManyCheckbox value="#{test.questionAnswerList}"
                    layout="pageDirection">
                    <f:selectItems value="#{question.questionAnswers}" var="ans"
                        itemValue="#{ans.questionAnswerIdentifer.questionId} #{ans.questionAnswerIdentifer.answerId}"
                        itemLabel="#{ans.answer.answer}" />
                </p:selectManyCheckbox>
            </ui:repeat>

但这对我无法帮助我List<String>只能在<ui:repeat/>中找到最后一个问题的问题答案。似乎在每次迭代后,questionAnswerList变为空

//好的,我读了一篇文章Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop

我决定使用地图Map&gt;

但是在这部分错误ClassCastException字符串不能转换为List

public List<String> getQuestionAnswerList() {
    // return questionAnswerList;
    List<String> selectedQuestionAnswer = new ArrayList<String>();
    if (!selectedItems.isEmpty()) {
        Set<Long> idsSet = selectedItems.keySet();
        for(Long questionId : idsSet){              
            List<String> questionAnswerPair = selectedItems.get(questionId);
            selectedQuestionAnswer.addAll(questionAnswerPair);
        }
    }
    return selectedQuestionAnswer;
}
<p:selectManyCheckbox value="#{test.selectedItems[question.questionId]}"
                    layout="pageDirection">
                    <f:selectItems value="#{question.questionAnswers}" var="ans"
                        itemValue="#{ans.questionAnswerIdentifer.questionId} #{ans.questionAnswerIdentifer.answerId}"
                        itemLabel="#{ans.answer.answer}" />
                </p:selectManyCheckbox> 

如何在List<String>中获取所有选择的值?

1 个答案:

答案 0 :(得分:1)

您可以制作一张地图(例如称之为myMap),其中密钥为entry.key,值为questionAnswerList

的相同类型的列表

类似

<p:selectManyCheckbox value="#{test.myMap[entry.key].questionAnswerList}"
    layout="pageDirection">
    <f:selectItems value="#{entry.value}" var="ans"
        itemValue="#{entry.key.questionId} #{ans.answer.answerId}"
        itemLabel="${ans.answer.answer}" />
</p:selectManyCheckbox>