gpt4 book ai didi

java - JSF h :selectManyCheckbox inside ui:repeat via Map entry

转载 作者:行者123 更新时间:2023-12-01 15:01:41 28 4
gpt4 key购买 nike

当我将值设置为 String 时,我遇到了问题进入List<String>里面<ui:repeat>当我按下按钮时,我看到 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>

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

//已更新我在不使用 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 Map>

但是在这部分错误ClassCastException字符串无法转换为列表

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>中所有选择的值?

最佳答案

您可以制作一个 map (例如,将其称为 myMap),其中键为 entry.key,值将是与 相同类型的列表>问题答案列表

类似的东西

<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>

关于java - JSF h :selectManyCheckbox inside ui:repeat via Map entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13549798/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com