【问题标题】:JSF h:selectManyCheckbox inside ui:repeat via Map entryJSF h:selectManyCheckbox inside ui:repeat via Map entry
【发布时间】:2012-11-25 09:33:44
【问题描述】:

我遇到了问题,当我将值作为 String 放入 <ui:repeat> 内的 List<String> 时,当我按下按钮时,我看到 List<String> 只包含 ui:repeat 的最后一个元素的记录。

<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 的想法

//已更新 我在不使用地图的情况下重写了这段代码

<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&lt;String&gt;,只需将&lt;ui:repeat/&gt; 中最后一个问题的问题答案配对。好像每次迭代之后 questionAnswerList 都变空了

//好吧,我看了一些文章Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop

我决定使用地图地图>

但在这部分错误ClassCastException String cannot be cast to 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&lt;String&gt; 中获取所有选择的值?

【问题讨论】:

  • @Daniel 谢谢,但没有帮助(
  • 我的意思不是说您一般不应该使用$...不是作为答案...您应该创建一个地图来保存每个问题的选择

标签: java jsf jsf-2 map


【解决方案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>

【讨论】:

  • @Ok 我试着照你说的做,但遇到其他问题,见上文
  • @Ray 而不是 itemValue="#{ans.questionAnswerIdentifer.questionId} #{ans.questionAnswerIdentifer.answerId}" 使 itemValue 成为 questionAnswerList 类型 &lt;someType&gt; 中存在的类型,因为现在 "#{ans.questionAnswerIdentifer.questionId} #{ans.questionAnswerIdentifer.answerId}" 是一个字符串
  • 这对我不起作用。我得到一个对象数组,但无法转换为 myObjectType。 @Daniel 你知道这个例子还缺少什么吗?我可以通过电子邮件或其他方式来处理我的代码。谢谢
  • @allegjdm93,我建议你发布一个新问题
猜你喜欢
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
  • 2012-06-22
  • 2014-05-30
  • 2013-03-19
  • 1970-01-01
  • 2023-04-07
  • 2013-07-02
相关资源
最近更新 更多