p:autoComplete itemLabel抛出“类'java.lang.String'没有属性'label'." [英] p:autoComplete itemLabel throws "The class 'java.lang.String' does not have the property 'label'."

查看:146
本文介绍了p:autoComplete itemLabel抛出“类'java.lang.String'没有属性'label'."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从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'.

/resources/components/popups/popupBuscaPessoa.xhtml @35,41 itemLabel="#{pessoa.label}": The class 'java.lang.String' does not have the property '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<SelectItem>一起使用.您应该用List<Pessoa>喂它.您也不应专注于转换SelectItem.您应该集中精力转换项目值,即Pessoa. SelectItem是旧版JSF 1.x的遗留物.在JSF 2.x中,由于视图中的varitemValueitemLabel属性,这不再是必需的.这样可以使您的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.

仅当您使用itemValue="#{pessoa}"并且#{modeloPopupBuscaPessoa.itemSelecionado}引用Pessoa属性时,才需要Converter.然后,您应该在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.

  • PrimeFaces showcase: <p:autoComplete> with POJO
  • Can I use omnifaces generic converter in primefaces autocomplete component?
  • Conversion Error setting value for 'null Converter'

这篇关于p:autoComplete itemLabel抛出“类'java.lang.String'没有属性'label'."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆