在Jsf 2.0中获取会话作用域的当前实例

时间:2011-09-30 06:42:46

标签: jsf-2

我提到了这个问题,我遇到了类似的问题 JSF - Get the SessionScoped Bean instance

我想在另一个托管bean中获取托管bean的当前实例。我有一个在基类 - baseBean中扩展的SuperBean。 我在baseBean中设置了list - itemList的值。列表的getter setter在SuperBean中,我应该在BackingBean中使用这个SuperBean来获取itemList的值。

我尝试使用 -

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

当我尝试打印superclass.getItems(); - 它只给出了这个 - []

还有这个 -

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

当我尝试使用它打印时 - 它抛出异常,因为我的superbean条目不存在于sessionMap中,即使该条目位于facesConfig中,也作为@ManagedBean和@sessionscoped

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

如果注入的bean具有相同或更宽的范围,则可以将托管bean注入另一个bean。这是一个例子:

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

    // don't forget to add getter and setter for anotherBean
    ...
}
相关问题