How to use Primefaces autocomplete with POJO list and POJO property as select value

In our current project, we want to replace <h:selectOneMenu>

with Primefaces <p:autocomplete>

. The select elements are Pojos list (JPA objects). The difference from the given examples in the examples shows that we want the primary key property (id) of the object to be selected as a selection value, so it can easily be passed as a parameter of the form:

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

      

Unfortunately, the above example will result in a PropertyNotFoundException:

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

The problem is that the attribute var

is of type Long

, not Player

. When used simple, <h:selectMenu>

it works in combination with <f:selectItems>

:

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

      

Does anyone know how to deal with this problem?

+3


source to share


1 answer


You can add a backing bean method that returns the player that belongs to the currently active playerId

one and set this attribute currentPlayer

to the value of your backing bean:

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

      



And in the view:

<p:autoComplete var="player" itemLabel="#{player.name}" itemValue="#{player}"
        completeMethod="#{playerBean.completePlayer}" forceSelection="true"
        converter="#{playerConverter}" 
        value="#{playerPreRenderViewListener.currentPlayer}">

      

+2


source







All Articles