Jsf Primefaces使用POJO和字符串值自动完成
我需要一个字符串值的自动完成,因为用户不能被自动完成方法限制为提供的项目,但他们应该能够在搜索字段中写入任何内容。如果他们愿意,他们也可以从建议中选择 现在我总是越来越紧张 /archive/overview.xhtml@28,57 itemLabel=“#{item.name}”:类“java.lang.String”没有属性“name”。 XHTML:Jsf Primefaces使用POJO和字符串值自动完成,jsf,jsf-2,primefaces,autocomplete,converter,Jsf,Jsf 2,Primefaces,Autocomplete,Converter,我需要一个字符串值的自动完成,因为用户不能被自动完成方法限制为提供的项目,但他们应该能够在搜索字段中写入任何内容。如果他们愿意,他们也可以从建议中选择 现在我总是越来越紧张 /archive/overview.xhtml@28,57 itemLabel=“#{item.name}”:类“java.lang.String”没有属性“name”。 XHTML: <p:autoComplete id="vyraz" value="#{archiveView.searchString}" co
<p:autoComplete id="vyraz" value="#{archiveView.searchString}"
completeMethod="#{archiveView.autocomplete}"
var="item" itemLabel="#{item.name}" itemValue="#{item.name}"
converter="archiveConverter" forceSelection="false" minQueryLength="2"
autoHighlight="false" effect="fade">
<p:column>
<h:outputText value="#{item.name}"/>
<h:outputText value=" (Barcode: #{item.barcode})" rendered="#{item.barcode ne null}"/>
</p:column>
<p:column>
<h:outputText value="#{item.type.label}" style="font-weight: bold;"/>
</p:column>
</p:autoComplete>
豆子:
private String searchString;//+接二连三
公共列表自动完成(字符串查询){
//从lucene索引/数据库获取并返回
}
是否有办法实现这一点(Primefaces 5.2)
谢谢
p:autocomplete
中的itemValue
属性可以作为转换器的轻量级替代品,仅在简单的情况下,当您不执行p:autocomple小部件的任何更新/刷新时(这基本上意味着您无法执行update=“@form”
或类似操作)
所以基本上有三种情况:
Pojo+转换器
必须将attributeuevar
设置为某个表达式,才能在PrimeFaces中启用“pojo模式”
<p:autoComplete
value="#{backingBean.myPojo}"
completeMethod="#{backingBean.autocomplete}
var="pojo" itemLabel="#{pojo.label}"
itemValue="#{pojo}" converter="pojoConverter">
</p:autoComplete>
Pojo+字符串
在本例中,您有一个pojo,其中有一个字符串字段要提取并在支持bean中使用
<p:autoComplete value="#{backingBean.myStringValue}"
completeMethod="#{backingBean.autocomplete}
var="pojo" itemLabel="#{pojo.label}"
itemValue="#{pojo.stringKey}">
</p:autoComplete>
我已经解决了移除转换器并将以下内容用作标签/值的问题:itemLabel=“#{item.class.simpleName eq'String'?item:item.name}”itemValue=“#{item.class.simpleName eq'String'?item:item.name}”非常有用,比链接答案更详细。在Pojo+字符串部分,必须删除转换器,否则会出现ClassCastException问题仍未解决。。。对itemLabel的重新评估使用字符串所需的值和提供POJO的完整方法来打破每个实现。。。这似乎是不必要的,而且多年来一直是人们的首选。。。
public interface Converter {
// @return *K* the value to be used in html
// @param obj is provided by the expression (itemValue="#{pojo}")
public String getAsString(FacesContext context, UIComponent component, Object obj);
// build the pojo identified by String *K*
// @param value *K*
public Object getAsObject(FacesContext context, UIComponent component, String value);
}
<p:autoComplete value="#{backingBean.myStringValue}"
completeMethod="#{backingBean.autocomplete}
var="pojo" itemLabel="#{pojo.label}"
itemValue="#{pojo.stringKey}">
</p:autoComplete>