Access JSF managed bean values ​​in another managed bean

I have a reporting page in which I have several filters, such as countryId , Date and several other options for the user to select. Now, based on the selected parameters, there is a database call that uses these parameters to retrieve a list of results.

Now the managed bean contains all of these search parameters and a list of results. Let's call it bean like Bean1

 public class Bean1 implements Constants{ private List<SelectItem> countryList; private List<String> choosenCountryList; private List<String> choosenProgramList; private String invoiceDatePriorTo= CalendarUtilities.getTodaysDate() ; private List<CustomResults> searchResultList } 

We have another managed bean Bean2 that contains the Bean1 property

 public class Bean2 implements Constants { private Bean1 bean1; public getSearchResults(){ //Code for fetching the search list for bean 1 this.setsearchResultList() //=fetched list from DB; } public modifySearchResults(){} } 

Now that we have the getSearchResults() method on the action trigger from the JSF page, and we set the searchResultList to be displayed on the screen. So we can display the search list on the screen

Now the list that we receive is subject to user change on the screen. Now, when we again call editSearchResults to edit the list, we cannot get the list in bean2, because the managed bean is in the request area.

Can someone tell me how to go further and solve this problem?

+6
source share
3 answers

Just declare your dataBean as managedProperty

From the tags I guess it about JSF2.0

You need to declare bean1 as a managed property in bean2

It should look like

 @ManagedBean public class Bean1{ } 

and

 @ManagedBean public class Bean2{ @ManagedProperty(value="#{bean1}") Bean1 bean1; //setter & getter of bean1 } 

see also

+11
source

Well, you said it yourself. You must move the bean from RequestScope .

Depending on your application, you can use:

  • @SessionScoped
  • @ViewScoped

Here you can find an article about choosing the right area.

Secondly, you can use Injection Dependency. JSF has a nice @ManagedProperty . You can read about it here .

EDIT

Seeing now that you do not want to use the session, another solution that comes to my mind is the following:

 @ManagedBean @ViewScoped public class Bean1 implements Constants{ private List<SelectItem> countryList; private List<String> choosenCountryList; private List<String> choosenProgramList; private String invoiceDatePriorTo= CalendarUtilities.getTodaysDate() ; private List<CustomResults> searchResultList public getSearchResults(){ // Your code here } public modifySearchResults(){ // Your code here } } 

As you said, the problem is that you cannot save all the information from one request to another. This is usually the case when @ApplicationScoped or @ViewScoped are commonly used. For various reasons, this may not be the best solution in some cases.

For your example, the best solution would be to place all the methods in one bean, which is @ViewScoped . This approach has its drawbacks, but I think it is as good as it gets.

Here you can find out more about this.

+4
source

Source: https://habr.com/ru/post/910167/


All Articles