6
votes

I have a managed bean called UserSearchHandler, it has a doSearch method that populates UserSearchHandler.searchResults which are displayed in a table on the userSearch.xhtml page.

I have another managed bean called UserHandler, it has a showUser method and so on.

In the search results table, the user name is a link that, when clicked, is supposed to show user details on a userView.xhtml page. The table and link looks like this:

<p:dataTable var="user" value="#{userSearchHandler.searchResults" >

// ... and so on ... then

<h:commandLink value="#{user.firstName}" action="#{userHandler.showUser}">
  <f:setPropertyActionListener target="#{userHandler.userIdToShow}" value="#{profile.id}"/>
</h:commandLink>

Everything works fine when the managed beans are set to session scope.

However, when I change the scope on the beans to request, the search works and the table gets populated, but when I click on the name link nothing happens. I put a break point on the userHandler.showUser method and it never gets hit when the userSearchHandler is set to "request" scope.

Can anyone help explain why this is, or what I'm doing wrong?

2

2 Answers

5
votes

That's because the #{userSearchHandler.searchResults} is empty during the new request and therefore JSF is unable to locate the associated row where the commandlink is been invoked in order invoke the action (and to pass/set properties if any).

You need to ensure that the same #{userSearchHandler.searchResults} is precreated during bean's construction/initialization. If it's to be created based on a specific set of parameters, then you've to pass them along with the form as well.

That's exactly the reason why solutions like Tomahawk's <t:saveState /> and new JSF 2.0 view scope exist.

See also:

0
votes

I have a couple of ideas. If you're using a in your navigation you can try taking that out. Doing so would mean the browser will not make a new HTTP request when it renders the second window. It is the new HTTP request which clears the request scoped beans by. If that is not an option, you may be able to pass a parameter in your link such as a record id, which could allow you to pull data from your data source matching that id.