这是 p:autocomplete 与实体转换器的示例:
脸 :
<p:autoComplete id="autocomplete1" value="#{mBAutoComplete.selectedEntity}" completeMethod="#{mBAutoComplete.complete}"
forceSelection="true" minQueryLength="3" maxResults="10">
<p:ajax update="autocomplete1" event="itemSelect" listener="#{mBAutoComplete.updateEntity}" />
</p:autoComplete>
转换器:
@FacesConverter(forClass = Entity.class,value = "entityConveter")
public class EntityConverter implements Converter {
public EntityConverter() {
}
@Override
public Entity getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
// get Entity with WebService, but you can get it with other ways :
// like calling EJB methods or using a Collection of Entity provided in
// another managed bean
return services.getWebService().getEntity(Integer.valueOf(arg2));
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
return String.valueOf(((Entity) arg2).getEntityID());
}
}
托管豆:
@ManagedBean(name = "mBAutoComplete")
@SessionScoped
public class MBAutoComplete {
// Entity
private Entity selectedEntity;
// Entity Autocomplete
public List<String> complete(String query) {
List<String> results = new ArrayList<String>();
for (Entity en : this.eqList) {
if (en.getEntityID().startsWith(query)) {
results.add(en.getEntityID());
}
}
return results;
}
public void updateEntity(SelectEvent e) {
// Don't forget to check null Object sent by SelectEvent
this.selectedEntity = (Entity) e.getObject();
}
// GETTERS & SETTERS
public void setSelectedEntity(Entity e) {
this.selectedEntity = e;
}
public Entity getSelectedEntity() {
return this.selectedEntity();
}
}
在此示例中,您不需要使用 FacesContext,只需使用 SelectEvent 和 Converter。
希望这有帮助!