jsf - p :autoComplete itemLabel throws "The class ' java. lang.String' 没有属性 'label' 。”

标签 jsf primefaces jsf-2 label converters

我正在从 IceFaces 更改为 PrimeFaces(我真的很想更改为 RichFaces,但会在新版本中导致错误,我不会),并且我在正确实现 primefaces 自动完成方面遇到了一些困难。根据他的手册,我只需要实现一个返回对象列表的方法,在这种情况下需要一个转换器。

我返回的列表是 javax.faces.model.SelectItem 的列表,我真的不明白为什么我需要为此创建一个转换器,但让我们继续。我创建了一个简单的转换器只是为了测试,但 primefaces 无法识别我的转换器并在浏览器中返回此错误:

/resources/components/popups/popupBuscaPessoa.xhtml @35,41 itemLabel="#{pessoa.label}": The class 'java.lang.String' does not have the property 'label'.

这是我的转换器类(只是为了测试):

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:

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

我做错了什么吗? SelectItem 没有默认转换器吗?有没有更简单的方法来实现这个转换器?

最佳答案

你不应该给它喂List<SelectItem> 。你应该喂它 List<Pessoa> 。您也不应该专注于转换 SelectItem 。您应该集中精力转换项目值,即 PessoaSelectItem是旧的 JSF 1.x 时代的遗留物。在 JSF 2.x 中,这不再是强制性的,这要归功于 var , itemValueitemLabel View 中的属性。这可以让您的 Bean 免受特定于 View 的困惑的影响。

Converter仅当您使用 itemValue="#{pessoa}" 时才需要和 #{modeloPopupBuscaPessoa.itemSelecionado}Pessoa属性(property)。然后你应该在 getAsString()转换Pessoa以其独特的String表示(以便可以以 HTML 格式打印)和 getAsObject() 中从 String 转换至Pessoa (以便可以在 bean 属性中设置)。

但是,如果 #{pessoa.value}String#{modeloPopupBuscaPessoa.itemSelecionado}也是String ,那么你应该使用 itemValue="#{pessoa.value}"并删除 Converter总共。

另请参阅:

关于jsf - p :autoComplete itemLabel throws "The class ' java. lang.String' 没有属性 'label' 。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7653698/

相关文章:

java - JSF 2.0; escape ="false"防止 XSS 的替代方案?

javascript - 使用 jQuery 更改使用 JSF 创建的表的单元格

java - 如何从请求 bean 更改 session bean 的值?

jsf-2 - Primefaces 数据表,多行项目

java - Simtay SimpleCRUD 上解析文件 :LazyUserDataModel. java 时出错

jsf - 如何使JSF inputText字段在模糊时变为大写

java - JSF-t :saveState x @ConversationScope

java - 类型 "Interface"与注入(inject)点 [BackedAnnotatedField] @Inject "Implementation"处的限定符 @Default 的依赖关系不满足

jsf - org.primefaces.component.menubar.MenubarRenderer.encodeSubmenuIcon 处的 java.lang.NullPointerException

java - <f :ajax> multiple call of Java method - how to do it only once?