我有一个有答案的问句循环。我想填写地图Map>选中的答案值
<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;
}但是我仍然得到同样的错误验证(我不明白为什么我会得到这个错误
发布于 2012-11-26 13:03:36
您需要创建一个Converter,它在Answer实例与其唯一的String表示之间进行转换,并通过<p:selectManyCheckbox>的converter属性引用它。
这里有一个启动示例(运行时检查省略了),条件是Answer有一个代表唯一技术标识符的id属性。
@FacesConverter("answerConverter")
public class AnswerConverter implements Converter {
@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) throws ConverterException {
// Write code to convert Answer to its unique String representation for usage in HTML/HTTP. E.g.
return String.valueOf(((Answer) modelValue).getId());
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException {
// Write code to convert unique String representation of Answer to concrete Answer for usage in Java/JSF. E.g.
return yourAnswerService.find(Long.valueOf(submittedValue));
}
}请注意,在运行时,@FacesConverter(forClass=Answer.class)不会作为泛型类型信息丢失。
另请参阅:
https://stackoverflow.com/questions/13562667
复制相似问题