Link to home
Start Free TrialLog in
Avatar of ChrisOz2008
ChrisOz2008Flag for Australia

asked on

Transferring objects between JSF pages

I have a JSF page that lists all active users on the system .Next to each user I have an Edit button. The page has a backing bean so in the page  use the following code to transfer the selected user to the backing bean:

<a4j:commandButton value="#{msgs.editBtn}" type="submit" reRender="dtable,table"
      alt="Click to Edit" action="#{UserListBean.edit}" id="editbtn" onclick="this.disabled=true" oncomplete="this.disabled=false">
<a4j:actionparam name="selectedItem" value="#{user.uniqueid}" assignTo="#{UserListBean.selecteditem}"/>
</a4j:commandButton>

This successfully sets the selected user key to the backing bean.  Next the page will return result that causes the system to follow the page flow to a new page where you wold edit the selected user.

The problem is that once on the new edit page, I can't transfer the selected user from the user list backing bean to the user edit backing bean.

My thinking was that in the setSelectedUser method of the user list backing bean I will just set a session attribute holding the selected user object, then in the new user edit page I can just read that attribute back from the session and off I go.

Strangely  in the new page I cannot retrieve the session attribute even though my JSP tag is validating correctly. However if I create a method in my taglib and use that tag in the page the user object is successfully retrieved.

I'm thinking that my logic must be wrong, how is this usually done?
Avatar of a_b
a_b

I would suggest that you retrieve the user details again from the db instead of transferring the user. In the scenario that someone else modifies the user data in the background you might be stuck with a stale object, so what you can do is to select the user, get the id and then do a fetch, set the data object that you want to work on in in the edit page.
Avatar of ChrisOz2008

ASKER

I considered doing that however I still need to transfer the ID of the user to the new page and once I have done that I am not sure how I ca cause the fetch to occur when the new page loads.
If you call a backing bean method to be called which loads the user when you pass the data id, that should so the trick. Precondition is to make sure you can pass the userid.
The problem is that if I have my backing bean on the list page instantiate backing bean for the next page that does as you say , then when the page is displayed then the that page wold have a different instance of the user edit page backing bean.

Or are you saying to use the same bean on both pages?
ASKER CERTIFIED SOLUTION
Avatar of a_b
a_b

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial