0

如何检索primefaces自动完成的选定值

<p:autoComplete value="#{completeBean.selected1}"  
 id="basicPojo1" completeMethod="#{completeBean.completePlayer}"
 var="p" itemLabel="#{p.getMateriel().getNumparc()}" itemValue="#{p}" 
 converter="numparc" forceSelection="true"> 
    <f:ajax   execute="basicPojo1" listener="#{evenEdl.submitListener}" render="@all"/>
</p:autoComplete>


public void submitListener(AjaxBehaviorEvent event) {
    String id = event.getComponent().getId();
    switch (id) { 
        case "basicPojo1":
            String parametre = FacesContext.getCurrentInstance().getExternalContext()
                                      .getRequestParameterMap().get("basicPojo1");
            System.out.println("test param"+parametre);
                 break;
    }
}
4

1 回答 1

1

这是 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。

希望这有帮助!

于 2013-06-15T10:36:07.623 回答