I want to build a p:autocomplete for searching different objects from a entities.
Like customer, article....
The searching works well and the entities will appear.
But still the converter doesn′t work.
Here my code:
<p:autoComplete id="searchStartpage" size="35" maxlength="200"
queryDelay="0" maxResults="10" minQueryLength="1"
value="#{searchGeneralRequestController.selectedObject}"
completeMethod="#{searchGeneralRequestController.completeObject}"
var="o" itemLabel="#{object.id}" itemValue="#{object}"
converter="objectForSearchConverter" forceSelection="false"
itemtipMyPosition="left center" cache="false"
itemtipAtPosition="right center">
<p:ajax event="itemSelect"
listener="#{searchGeneralRequestController.handleSelect}" />
<p:column rendered="#{o.getClass().getSimpleName() == 'Mandatory'}">
#{o.surname} #{o.name}
</p:column>
<p:column rendered="#{o.getClass().getSimpleName() == 'Article'}">
#{o.name}
</p:column>
</p:autoComplete>
And here my Converter:
public Object getAsObject(FacesContext facesContext, UIComponent component,
String submittedValue) {
if (submittedValue.trim().equals("")) {
return null;
} else {
try {
// searchValue = submittedValue;
int number = Integer.parseInt(submittedValue);
for (Object p : objectList) {
if (p instanceof Article) {
Article article = (Article) p;
if (article.getId() == number) {
return p;
}
}
if (p instanceof Customer) {
Customer customer = (Customer) p;
if (customer.getId() == number) {
return p;
}
}
if (p instanceof User) {
User user = (User) p;
if (user.getId() == number) {
return p;
}
}
if (p instanceof Mandatory) {
Mandatory mandatory = (Mandatory) p;
if (mandatory.getId() == number) {
return p;
}
}
}
} catch (NumberFormatException exception) {
throw new ConverterException(new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Conversion Error",
"Not a valid player"));
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
if (value == null || value.equals("")) {
return "";
} else {
Long id = (value instanceof Mandatory) ? ((Mandatory) value)
.getId() : null;
return (id != null) ? String.valueOf(id) : null;
}
}
All my entities have hash and string method.
The Object value is always null
Can anybody help, please
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…