1

我通过带有答案的问题进行了循环。我要填写地图地图>选中的答案值

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

但我仍然得到相同的错误验证(我不明白为什么我会收到这个错误

4

1 回答 1

2

您需要创建一个在实例及其唯一表示Converter之间转换的,并通过 的属性引用它。AnswerStringconverter<p:selectManyCheckbox>

这是一个启动示例(省略了运行时检查),前提是它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)由于泛型类型信息在运行时丢失,因此它不起作用。

也可以看看:

于 2012-11-26T13:03:36.830 回答