Reputation: 1828
I have a loop via qusetions with answers. I want to fill map Map> checked Answers values
<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>
In my bean I have
private Map<Long, List<Answer>> selectedItems;
private List<Question> questionList;
private Map<Long, List<Answer>> questionAnswerMap;
//getter- setter selectedItems
and
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;
}
Also I show my model class
public class Question implements Serializable{
private Long questionId;
private String question;
private Integer complexity;
private Set<QuestionAnswer> questionAnswers;
}
where QuestionAnswer
also model class like this
public class QuestionAnswer implements Serializable{
private QuestionAnswerIdentifer questionAnswerIdentifer;
private Answer answer;
}
QuestionAnswerIdentifer class which service as componentId and consist from Long answerId, Long questionId
When I push 'pass test button' I get error
j_id280458803_1_639e37a6:0:j_id280458803_1_639e37cf:
Validation Error: Value is not valid
///UPDATED I try BalusC answer and write converter
@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;
}
}
//equals and hashCode for Answer
@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;
}
But I still get the same error validation ( I don't understand why I get this error
Upvotes: 1
Views: 1114
Reputation: 1108632
You need to create a Converter
which converts between the Answer
instance and its unique String
representation and reference it by converter
attribute of <p:selectManyCheckbox>
.
Here's a kickoff example (runtime checks omitted), provided that Answer
has an id
property representing the unique technical identifier.
@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));
}
}
Note that the @FacesConverter(forClass=Answer.class)
won't work as generic type information is lost during runtime.
Upvotes: 2