JSF – P: autocomplete itemlabel throws “class’ Java. Lang. string ‘has no attribute’ label ‘.”

I'm changing from icefaces to primefaces (I really want to change RichFaces, but I won't cause errors in the new version) and I'm trying to implement the correct primefaces autocomplete According to his manual, I only need to implement a method to return the object list. In this case, I need a converter

The list I'm returning is javax faces. model. I really don't understand why I need to create a converter for this, but let's continue I created a simple converter to test, but primefaces could not recognize my converter and returned this error in the browser:

This is my conversion course (just for testing):

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,Object object) {
    return ((SelectItem)object).getLabel();
}
}

This is where I tried to use p: autocomplete:

<p:autoComplete value="#{modeloPopupBuscaPessoa.itemSelecionado}"
            completeMethod="#{controladorSugestaoPessoa.atualizarSugestoes}"
            var="pessoa" itemLabel="#{pessoa.label}" itemValue="#{pessoa.value}"
            converter="#{conversorSelectItem}"/>

Did I do anything wrong? Is there a default converter for selectitem? Is there a simpler way to implement this converter?

Solution

You should not use list < selectitem > to provide it You should feed it with list < Pessoa > Nor should you focus on converting selectitem You should focus on converting the project value, Pessoa Selectitem is old JSF 1 The legacy of the x age In JSF 2 In X, this is no longer mandatory due to the VaR, itemvalue and itemlabel attributes in the view This keeps the beans clean and avoids view - specific clutter

The converter is required as long as you use itemvalue = "#{Pessoa}" and #{modelopopupbuscapessoa. Itemselecionado} references the Pessoa property Then, you should convert Pessoa to its unique string representation in getasstring() (so that it can be printed in HTML) and string to Pessoa in getasobject() (so that it can be set in bean properties)

However, if #{Pessoa. Value} is a string and #{modelopoupbuscapessoa. Itemselecionado} is also a string, you should only use itemvalue = "#{Pessoa. Value}" and delete the converter completely

You can also see:

> PrimeFaces showcase: with POJO > Can I use omnifaces generic converter in primefaces autocomplete component? > Conversion Error setting value for ‘null Converter’

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>