0
votes

i referred to this question and i've a similar problem JSF - Get the SessionScoped Bean instance

I want to get the current instance of a managed bean in another managed bean. I've a SuperBean which is extended in a base Class - baseBean. I set the value of a list - itemList in baseBean. The getter setter of the list are in SuperBean and I should use this SuperBean in my BackingBean to get the value of the itemList.

I tried using -

     Application app = FacesContext.getCurrentInstance().getApplication();
      ValueBinding vb = app.createValueBinding("#{superbean}");
      SuperClass superclass = (SuperClass) vb.getValue(FacesContext.getCurrentInstance());

When I try to print superclass.getItems(); - It gives only this - []

And also this -

  SuperClass superclass = (SuperClass)FacesContext.getCurrentInstance().
                         getExternalContext().getSessionMap().get("superbean");

When I try to print with this - It throws exception as my superbean entry is not present in the sessionMap even though the entry is placed in facesConfig and also as @ManagedBean and @sessionscoped

Kindly help me resolve this.

1
Doesn't the solution of the referred question help? I think you won't need the SuperBean if you use the @ManagedProperty annotation. I think your superbean won't be intantiated if you only reference properties from sub classes. - Matt Handy
@ManagedProperty annotation has to be given in my BackingBean is it? - JaveDeveloper
Added an answer with an example - Matt Handy
You need to concentrate on the problem why it returns an empty list, not on how you grab it from the context. Using @ManagedProperty should be perfectly fine. Perhaps you've just created a completely different instance manually? - BalusC
@BalusC Congratulations for hitting the 200k! - Matt Handy

1 Answers

2
votes

You can inject a managed bean into another if the injected bean has the same or broader scope. Here is an example:

@ManagedBean(name = "oneBean")
@ViewScoped
public class OneBean{
    // injections
    @ManagedProperty(value = "#{anotherBean}")
    private AnotherBean anotherBean;

    // don't forget to add getter and setter for anotherBean
    ...
}