开发者

To get current Instance of the session scoped bean in Jsf 2.0

开发者 https://www.devze.com 2023-04-09 06:07 出处:网络
i referred to this questionand 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

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.


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
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号