3

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 showcases 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: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 example above will lead to a PropertyNotFoundException:

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

The problem is that the var attribute is of Type Long and not 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?

fischermatte
  • 3,327
  • 4
  • 42
  • 52
  • 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. – CSchulz Jun 17 '15 at 09:33

1 Answers1

2

You could add a backing bean method that returns the player belonging to the currently active playerId and set this currentPlayer as value attribute 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}">
Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • 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 Mar 09 '12 at 16:45
  • in the example above you mean `itemValue="#{player}"` i guess (without id)? – fischermatte Mar 09 '12 at 16:48
  • You could set the `currentPlayer` in the setter for `playerId`. Btw: What is your PlayerPreRenderViewListener? Is it a phase listener? – Matt Handy Mar 09 '12 at 16:58
  • 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 Mar 15 '12 at 20:37
  • just not so nice, that you have to define a `playerConverter` + a lookup method for the given playerId. when you use `h:selectMenu` a converter is enough. from my point of view this could be an approvement for primefaces autocomplete. – fischermatte Mar 15 '12 at 20:45
  • You could submit a [feature request](http://code.google.com/p/primefaces/issues/list) for this. You got my vote! – Matt Handy Mar 15 '12 at 21:10
  • okay, created issue [3750](http://code.google.com/p/primefaces/issues/detail?id=3750) – fischermatte Mar 16 '12 at 17:14
  • 1
    will u please provide code snippet of playerConverter – Subodh Bisht Jul 10 '14 at 12:58