Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2.0 pass data between beans (or pages?)

Tags:

jsf-2

I'm working with JSF 2.0

I have a form in my admin section where I am going to select some users in a list.

The form (selectusers.xhtml) is adding these users to a list in a bean (SelectUsers.java).

After I have selected some user(s), I will pass the list of the user(s) from SelectUsers.java to another bean (AddAddressBean.java) and continue add information in another form (addadress.xhtml) which set other properties related to AddAddressBean for each user.

I do not know how to implement it. I would like that AddAddressBean.java shall be independent (so I can use it together with other beans), so I prefer that AddAddressBean.java shall not know about other beans.

Can you please help me? =)

B.R Carl

like image 845
kungcc Avatar asked Feb 19 '11 19:02

kungcc


1 Answers

Several quick things come to mind :

  1. Perhaps you could have a single bean only for those related pages, using @SessionScoped or the shorter CDI's @ConversationScope, or and this is the best of the three, the DeltaSpike @ViewAccessScoped
  2. When clicking the button on page 1 where it'll take you to page 2, in the 1st bean, you can make use of Flash object to store objects you want to pass, and in the second bean's @PostConstruct method, you could get all the objects from the Flash object
  3. If you dont mind using session scope, you can still have 2 beans, and one bean can refer to another bean using the jsf way(@ManagedProperty), or the Java EE inject way(@Inject) or the spring way if you use spring (@Autowired)
like image 61
Albert Gan Avatar answered Oct 23 '22 03:10

Albert Gan