9
votes

I have a report generation page where I have few filters like countryId, Date and few other parameters for the user to select. Now based on the chosen parameters, there is a database call which uses these parameters to fetch a result list .

Now the managed bean contains all these search parameters and the result list .Let us name this bean as 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 one more managed bean Bean2 which contains a property of Bean1

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 when on the trigger of an action from the JSF page we call the getSearchResults() method and we set the searchResultList to be displayed in the screen .In this way we are able to display the search list on screen

Now the list we get is subjected to user modification on screen .Now when we again call the modifySearchResults to edit the list we are not able to retrieve the list in the bean2 because the managed bean is in request scope .

Can anyone tell me how to go ahead and solve this problem?

3

3 Answers

11
votes

Just declare your dataBean as ManagedProperty.

From the tagging I assume it's about JSF2.0.

You need to declare bean1 as 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

4
votes

Well you said it yourself. You have to move the bean from the RequestScope.

Depending of your application you may want to use:

  • @SessionScoped
  • @ViewScoped

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

Second of all you may want to use Dependency Injection. JSF has the nice @ManagedProperty. You can read more about it here.

EDIT

Seeing now that you don't want to use the Session another solution which 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 can't save all the information from one request to the next. Usually this is a case when normally the @ApplicationScoped or @ViewScoped is used. For different reasons this may not be the best solution in some cases.

For your example placing all the methods in one bean which is @ViewScoped may be the best solution. This approach comes with its drawbacks but I think this is as good as it gets..

Here you can read more about this matter.