PrimeFaces P:自动完成对象列表,转换失败
我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的代码,我能告诉您的是,您缺少自定义转换器类。
您应该像
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