how to get beans from view scope

前端 未结 1 1102
死守一世寂寞
死守一世寂寞 2020-12-13 21:21

I want to ask if i place my managed bean in session scope, then it is stored in session\' Like if i have a bean like this

@ManagedBean
@SessionScoped
public          


        
相关标签:
1条回答
  • 2020-12-13 22:06

    They're stored in the view map as available by UIViewRoot#getViewMap():

    Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
    ViewScopedBean viewScopedBean = (ViewScopedBean) viewMap.get("viewScopedBean");
    // ...
    

    Equivalently, you should be using the session map abstraction as well which is available by ExternalContext#getSessionMap() (you ultimately want to have zero javax.servlet import declarations throughout your JSF code):

    Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
    SessionScopedBean sessionScopedBean = (SessionScopedBean) sessionMap.get("sessionScopedBean");
    // ...
    

    See also:

    • Get JSF managed bean by name in any Servlet related class

    Unrelated to the concrete problem, this may not be the best way. Look at @ManagedProperty if possible.

    0 讨论(0)
提交回复
热议问题