This question already has an answer here:
I need to have an autocomplete with string value, because users can't be restricted to provided items by autocomplete method, but they should be able to write anything in the search field. If they want, they can choose from suggestions as well.
Now I am always getting /archive/overview.xhtml @28,57 itemLabel="#{item.name}": The class 'java.lang.String' does not have the property 'name'.
XHTML:
<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>
Bean:
private String searchString; // + getter and setter
public List<ArchiveAutoCompleteDto> autocomplete(String query) {
// get and return from lucene index/database
}
Is there a way to implement this (Primefaces 5.2)?
Thanks!
itemValue
property inp:autocomplete
can be used as a lightweight replacement of converters in just simple scenarios when you do not perform any update/refresh of the p:autocomple widget (which basically means you cannot performupdate="@form"
or similar)So basically there are 3 cases:
Pojo + Converter
Setting the attributue
var
to some expression is mandatory to enable the "pojo mode" in PrimeFaces.In this scenario
var="pojo"
is an instance of class A.value="#backingBean.myPojo}"
is a variable of type A.itemValue="#{pojo}"
is evaluated when you ask for the suggestion list, the result is passed to the converter viagetAsString
which produces the value to encode in html (eg.:v1
).When you select an element from the list (eg.:
v1
) it is passed back to the converter intogetAsObject
which gives you in return an object of type A to set in the backing bean. The converter as usual has full responsibility in translating from Pojo to HTML value and vice versa.Pojo + String
In this case you have a pojo with a String field to extract and to use in the backing bean.
The flow is the same but
Converter#getAsString
) and set to"#{backingBean.myStringValue}"
once selected."#{backingBean.myStringValue}"
must be a string of course.Everything works fine until you try to perform refresh the
p:autoComplete
widget (for example update="@form"). Primefaces re-evaluates theitemLabel
(because, for some reason, it does not store the itemLabel in the ViewState) using the value from the backing bean which is a String. Therefore you get the error. Actually there is no solution to this problem but to provide an implementation as in case 1).Plain String Values
Not covered here.