JSF: bean scope; session versus request

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 showUser method, etc.

In the search results table, the username is a link that, when clicked, should show user data on the userView.xhtml page. The table and link are as follows:

 <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 managed beans are set to session scope.

However, when I change the scope of the beans to request , the search works and the table fills, but when I click on the name link, nothing happens. I put a breakpoint on the userHandler.showUser method, and it never hits if the UserSearchHandler parameter UserSearchHandler set to a query scope.

Can someone explain why this is or what I am doing wrong?

+6
jsf
source share
2 answers

This is because #{userSearchHandler.searchResults} empty during a new request, and therefore JSF cannot find the corresponding line in which the command link was called to invoke the action (and pass / set properties, if any).

You need to make sure that the same #{userSearchHandler.searchResults} is created during the build / initialization bean. If it will be created based on a specific set of parameters, you must pass them along with the form.

It is for this reason that there are solutions such as Tomahawk <t:saveState /> and the new JSF 2.0 presentation area.

See also:

  • What are the main disadvantages of JSF 2.0?
+5
source share

I have a couple of ideas. If you use in your navigation, you can try this. This means that the browser will not create a new HTTP request when it displays a second window. This is a new HTTP request that clears the request with beans by scope. If this is not an option, you can pass a parameter in your link, such as a record identifier, which can allow you to retrieve data from your data source matching that id.

0
source share

All Articles