Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

jsf - p:autoComplete itemLabel throws "The class 'java.lang.String' does not have the property 'label'."

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.

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}": 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();
}
}

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}"/>

Did I do something wrong? Isn't there a default converter for SelectItem? Is there a easier way to implement this converter?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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).

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.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...