我通过带有答案的问题进行了循环。我要填写地图地图>选中的答案值
<ui:repeat value="#{test.questionList}" var="question">
<h:outputText value="#{question.question}"
rendered="#{not empty question}" />
<p:selectManyCheckbox value="#{test.selectedItems[question.questionId]}"
layout="pageDirection" converter="#{answerConverter}">
<f:selectItems value="#{question.questionAnswers}" var="ans"
itemValue="#{ans.answer}" itemLabel="#{ans.answer.answer}" />
</p:selectManyCheckbox>
</ui:repeat>
在我的豆子里,我有
private Map<Long, List<Answer>> selectedItems;
private List<Question> questionList;
private Map<Long, List<Answer>> questionAnswerMap;
//getter- setter selectedItems
和
public Map<Long, List<Answer>> getQuestionAnswerMap() {
if (!selectedItems.isEmpty()) {
Set<Long> idsSet = selectedItems.keySet();
for (Long questionId : idsSet) {
List<Answer> answersOnPassedQuestion = selectedItems
.get(questionId);
questionAnswerMap.put(questionId, answersOnPassedQuestion);
}
}
return questionAnswerMap;
}
public void setQuestionAnswerMap(Map<Long, List<Answer>> questionAnswerMap) {
this.questionAnswerMap = questionAnswerMap;
}
我还展示了我的模型课
public class Question implements Serializable{
private Long questionId;
private String question;
private Integer complexity;
private Set<QuestionAnswer> questionAnswers;
}
QuestionAnswer
像这样的模型类在哪里
public class QuestionAnswer implements Serializable{
private QuestionAnswerIdentifer questionAnswerIdentifer;
private Answer answer;
}
QuestionAnswerIdentifer 类作为 componentId 服务并由Long answerId, Long questionId
当我按下“通过测试按钮”时出现错误
j_id280458803_1_639e37a6:0:j_id280458803_1_639e37cf:
Validation Error: Value is not valid
///更新 我尝试 BalusC 回答并编写转换器
@ManagedBean
@RequestScoped
public class AnswerConverter implements Converter{
@ManagedProperty(value="#{answerService}")
private IAnswerService answerService;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue)
throws ConverterException {
Answer answer = new Answer();
try {
answer = answerService.getById(Long.valueOf(submittedValue));
} catch (DAOException | NumberFormatException e) {
e.printStackTrace();
}
return answer;
}
@Override
public String getAsString(FacesContext context, final UIComponent component, Object modelValue)
throws ConverterException {
return String.valueOf(((Answer) modelValue).getAnswerId());
}
public void setAnswerService(IAnswerService answerService) {
this.answerService = answerService;
}
}
//等于和hashCode作为答案
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((answer == null) ? 0 : answer.hashCode());
result = prime * result
+ ((answerId == null) ? 0 : answerId.hashCode());
result = prime * result
+ ((correctness == null) ? 0 : correctness.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Answer other = (Answer) obj;
if (answer == null) {
if (other.answer != null)
return false;
} else if (!answer.equals(other.answer))
return false;
if (answerId == null) {
if (other.answerId != null)
return false;
} else if (!answerId.equals(other.answerId))
return false;
if (correctness == null) {
if (other.correctness != null)
return false;
} else if (!correctness.equals(other.correctness))
return false;
return true;
}
但我仍然得到相同的错误验证(我不明白为什么我会收到这个错误