JSF 2.0 pass data between beans (or pages?)

拥有回忆 提交于 2019-12-21 05:11:21

问题


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


回答1:


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)



回答2:


This how i implemented (used ConversationScoped as @bertie said ).

bean 1:

@Named("conversationBean1")
@ConversationScoped
public class ConversationBean1 implements Serializable {
          //---start conversation----

  }

bean 2:

@Named("conversationBean2")
@ConversationScoped
public class ConversationBean2 implements Serializable 
  {
      @Inject
      private ConversationBean1 conversationBean1;
   }


来源:https://stackoverflow.com/questions/5052869/jsf-2-0-pass-data-between-beans-or-pages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!