Cómo utilizar el autocompletado de Primefaces con una lista de POJO y una propiedad de POJO como valor de selección

In our current project we want to replace a <h:selectOneMenu> with Primefaces's <p:autocomplete>. The select items are a list of Pojos (JPA Entities). The difference to the given examples in primefaces vitrinas is, that we want the primary key property (id) of the entity as selection value, so it can be easily passed as view param:

    <f:viewParam name="playerId" value="#{playerPreRenderViewListener.searchCriteria.playerId}" />
    <f:viewParam name="year" value="#{playerPreRenderViewListener.searchCriteria.year}" />
    <h:inputText value="#{playerPreRenderViewListener.searchCriteria.year}"/>
    <p:autoComplete var="player" itemLabel="#{player.name}" itemValue="#{player.id}"
        completeMethod="#{playerBean.completePlayer}" forceSelection="true"
    <h:commandButton value="Submit" action="showTeam?faces-redirect=true&amp;includeViewParams=true" />

Unfortunately the example above will lead to a PropertyNotFoundException:

itemLabel="#{player.name}": Property 'name' not found on type java.lang.Long'

El problema es que var attribute is of Type Long y no Player. When using a simple <h:selectMenu> it works in conjunction with <f:selectItems>:

<f:selectItems var="player" value="#{playerBean.listPlayers}" itemLabel="#{player.name}" itemValue="#{player.id}" />

Does anybody know how to handle this issue?

preguntado el 09 de marzo de 12 a las 15:03

Can you provide code snippet of your player converter, please? I have ran into the same issue and want to use a converter for this. -

1 Respuestas

You could add a backing bean method that returns the player belonging to the currently active playerId y configura esto currentPlayer as value attribute of your backing bean:

public Player getCurrentPlayer() {
  // find player by playerId and return

Y en la vista:

<p:autoComplete var="player" itemLabel="#{player.name}" itemValue="#{player}"
        completeMethod="#{playerBean.completePlayer}" forceSelection="true"

respondido 09 mar '12, 16:03

thx, but this would mean having one getter and on setter for the view params, and another getter and setter for the autocomplete. Plus - on each preRenderView you have to lookup the player object for the id given as viewparam. isnt there a simpler way, as we had it with h:selectOneMenu? - fischermatte

in the example above you mean itemValue="#{player}" i guess (without id)? - fischermatte

Podrías configurar el currentPlayer en el setter para playerId. Btw: What is your PlayerPreRenderViewListener? Is it a phase listener? - mate práctico

thx matt. the problem is, that in the code above searchCriteria is just a simple transfer object. itself can not lookup a player. in our use case players are fetched from db. PS: playerPreRenderViewListener is just a request scoped backing bean for initialisation on PreRenderViewEvent, could be playerBean as well. - fischermatte

will u please provide code snippet of playerConverter - Subodh

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.