PrimeFaces P:自动完成对象列表,转换失败

发布于 2025-01-29 10:40:47 字数 1235 浏览 1 评论 0原文

我使用p:autocomplete正确地遇到了一些麻烦,我试图让用户搜索基于对象的文本字段。目前,我有以下内容:

<p:autoComplete forceSelection="true" value="#{answer.destinationQuestion}"
                completeMethod="#{editorView.completeText}"
                var="destinationQuestion" itemLabel="#{destinationQuestion.questionText}"
                itemValue="#{destinationQuestion}"/>
public List<Question> completeText(String query) {
        List<Question> ret = new ArrayList<Question>();
        
        for(Section section : this.survey.getSections()) {
            for(Question question : section.getQuestion()) {
                if(question.getQuestionText() != null && question.getQuestionText().contains(query)) {
                    ret.add(question);
                }
            }
        }
        
        return ret;
    }

我对此代码的期望是,自动完成将让用户选择一个Question实例,但是一旦从autocomplete中选择了一个值并触发了更新事件,我就会接收以下例外:

java.lang.IllegalArgumentException: Cannot convert Question@228bb9f7 of type class java.lang.String to class Question

有人建议我使用一个转换器,但是我很困惑为什么选择的值被视为字符串而不是我的问题对象,因为itemValue被定义为问题对象。

编辑:这是在PrimeFaces 6.2上

I'm having some trouble using p:autoComplete properly, I am trying to let a user search based on a text field of an object. Currently I have the following:

<p:autoComplete forceSelection="true" value="#{answer.destinationQuestion}"
                completeMethod="#{editorView.completeText}"
                var="destinationQuestion" itemLabel="#{destinationQuestion.questionText}"
                itemValue="#{destinationQuestion}"/>
public List<Question> completeText(String query) {
        List<Question> ret = new ArrayList<Question>();
        
        for(Section section : this.survey.getSections()) {
            for(Question question : section.getQuestion()) {
                if(question.getQuestionText() != null && question.getQuestionText().contains(query)) {
                    ret.add(question);
                }
            }
        }
        
        return ret;
    }

My expectation with this code is that the autoComplete would let a user select a single Question instance, but once a value is selected from the autoComplete and an update event is triggered, I receive the following exception:

java.lang.IllegalArgumentException: Cannot convert Question@228bb9f7 of type class java.lang.String to class Question

It has been suggested that I use a converter, but I am confused why the value selected is being treated as a String rather than my Question object, since itemValue is defined to be a Question object.

Edit: This is on PrimeFaces 6.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆躺在深渊里 2025-02-05 10:40:47

根据您的代码,我能告诉您的是,您缺少自定义转换器类。
您应该像Converter =“#{QuestionConverter}”使用它。

有关进一步的参考,您可以看到 this

Based on your code, what I can tell you is, you are missing a custom converter class.
You should use it like converter="#{questionConverter}".

For further reference you can see this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文