【问题标题】:JSF selectManyCheckboxes doesn't workJSF selectManyCheckboxes 不起作用
【发布时间】:2012-11-26 10:15:19
【问题描述】:

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

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

作为componentId服务的QuestionAnswerIdentifer类,由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;
        }

但我仍然得到相同的错误验证( 我不明白为什么会出现此错误

【问题讨论】:

  • 如果您实际上没有使用 JSF 1.2,请不要使用 [jsf-1.2] 标签。
  • @BalusC 你说的是 var 而不是 #{item.value}
  • 您已在问题上添加了[jsf-1.2] 标签。如果您实际上没有使用 JSF 1.2,则不应这样做。我已经编辑了问题并删除了错误的标签。

标签: java jsf jsf-2 checkbox


【解决方案1】:

您需要创建一个Converter,它在Answer 实例及其唯一的String 表示之间进行转换,并通过&lt;p:selectManyCheckbox&gt;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) 将不起作用,因为泛型类型信息在运行时会丢失。

另见:

【讨论】:

  • 我无法理解问题的原因。这就是为什么我无法理解我应该写在Converter
  • 您将转换器称为converter="#{answerConverter}",对吗?您已经在 Answer 类中正确实现(或 IDE 自动生成的)equals()hashCode() 方法,对吧?
猜你喜欢
  • 2013-11-08
  • 1970-01-01
  • 2019-12-27
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 2011-11-26
  • 2011-02-09
相关资源
最近更新 更多