问题描述
我正在从 IceFaces 更改为 PrimeFaces(我真的很想更改为 RichFaces,但会导致新版本中出现错误,我不会)并且我在正确实现 PrimeFaces autoComplete 方面遇到了一些困难.根据他的手册,我只需要实现一个返回对象列表的方法,在这种情况下,需要一个转换器.
I'm changing from IceFaces to PrimeFaces (I really wanted to change to RichFaces but cause a bug in new version, I won't) and I'm havinng some dificults to implement correctly primefaces autoComplete. According to his manual I just need to implement a method that returns a list of objects, and in this case a converter is required.
我返回的列表是 javax.faces.model.SelectItem 的列表,我真的不明白为什么我需要为此创建一个转换器,但让我们继续.我创建了一个简单的转换器只是为了测试,但 primefaces 无法识别我的转换器并在浏览器中返回此错误:
The list I'm returning is a list of javax.faces.model.SelectItem, I really can't understand why I need to create a converter to this, but lets continue. I've created a simple converter just to test, but primefaces don't recognizes my converter and returns this error in browser:
/resources/components/popups/popupBuscaPessoa.xhtml @35,41 itemLabel="#{pessoa.label}":java.lang.String"类没有label"属性.
这是我的转换器类(仅用于测试):
This is my conversor class (just to test):
public class ConversorSelectItem implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value!=null && value.isEmpty())
return null;
SelectItem selectItem=new SelectItem();
selectItem.setLabel(value);
return selectItem;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object object) {
return ((SelectItem)object).getLabel();
}
}
这是我尝试使用 p:autocomplete 的地方:
This is where I try use p:autocomplete:
<p:autoComplete value="#{modeloPopupBuscaPessoa.itemSelecionado}"
completeMethod="#{controladorSugestaoPessoa.atualizarSugestoes}"
var="pessoa" itemLabel="#{pessoa.label}" itemValue="#{pessoa.value}"
converter="#{conversorSelectItem}"/>
我做错了吗?SelectItem 没有默认转换器吗?有没有更简单的方法来实现这个转换器?
Did I do something wrong? Isn't there a default converter for SelectItem? Is there a easier way to implement this converter?
推荐答案
你不应该用 List
来提供它.你应该用 List
来提供它.您也不应该专注于转换 SelectItem
.您应该专注于转换项目值,即Pessoa
.SelectItem
是旧 JSF 1.x 时代的遗留物.在 JSF 2.x 中,这不再是强制性的,这要归功于视图中的 var
、itemValue
和 itemLabel
属性.这使您的 bean 免受特定视图的混乱.
You shouldn't feed it with List<SelectItem>
. You should feed it with List<Pessoa>
. You should also not concentrate on converting SelectItem
. You should concentrate on converting the item value, which is Pessoa
. The SelectItem
is a leftover from the old JSF 1.x ages. In JSF 2.x this is not mandatory anymore, thanks to the var
, itemValue
and itemLabel
attributes in the view. This keeps your bean clean from view-specific clutter.
Converter
仅在您使用 itemValue="#{pessoa}"
并且 #{modeloPopupBuscaPessoa.itemSelecionado}
是指Pessoa
属性.然后,您应该在 getAsString()
中将 Pessoa
转换为其唯一的 String
表示(以便它可以在 HTML 中打印)和 getAsObject()
将 String
转换为 Pessoa
(以便在 bean 属性中设置).
The Converter
is only necessary whenever you use itemValue="#{pessoa}"
and the #{modeloPopupBuscaPessoa.itemSelecionado}
refers a Pessoa
property. You should then in getAsString()
convert Pessoa
to its unique String
representation (so that it can be printed in HTML) and in getAsObject()
convert from String
to Pessoa
(so that it can be set in bean property).
然而,如果 #{pessoa.value}
是一个 String
并且 #{modeloPopupBuscaPessoa.itemSelecionado}
也是一个 String
,那么您应该只使用 itemValue="#{pessoa.value}"
并完全删除 Converter
.
However, if #{pessoa.value}
is a String
and #{modeloPopupBuscaPessoa.itemSelecionado}
is also a String
, then you should just use itemValue="#{pessoa.value}"
and remove the Converter
altogether.
这篇关于p:autoComplete itemLabel 抛出“类 'java.lang.String' 没有属性 'label'."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!