【发布时间】: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<String>,只需将<ui:repeat/> 中最后一个问题的问题答案配对。好像每次迭代之后 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<String> 中获取所有选择的值?
【问题讨论】:
-
@Daniel 谢谢,但没有帮助(
-
我的意思不是说您一般不应该使用
$...不是作为答案...您应该创建一个地图来保存每个问题的选择