如何从视图范围获取豆类 [英] how to get beans from view scope
问题描述
我想问是否将托管bean放置在会话范围内,然后将其存储在会话中'就像我是否有这样的bean
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 class SessionScopedBean implements Serializable {
.......
} //end of class SessionScopedBean
然后将其存储在会话中,在我的会话中,我可以使用
Then it stored in the session, and during my session i can get it using
session.getAttribut("SessionScopedBean");
这将为我提供SessionScopedBean对象,并且当会话被销毁时,我将为null.现在,我想问一下是否在Scope中有我的bean,然后如何获取它.喜欢
This will give me the SessionScopedBean Object, and when session will get destroy, i will get null. Now i want to ask if i have my bean in view Scope, then how can i get it. Like
@ManagedBean
@ViewScoped
public class ViewScopedBean implements Serializable {
.......
} //end of class ViewScopedBean
现在如果视图是持久的,那么这个bean处于视图状态,并且当视图改变时,这个bean将被破坏.现在我想问一下,如果视图持久化,我该如何从视图状态中获取这个bean.喜欢
Now if the view is persist then this bean is in the view state, and when view changes, this bean will get destroy.Now i want to ask how can i get this bean from view state, if the view persist. Like
view.getAttrubute("ViewScopedBean"); //just a code. No actual implementation.
谢谢
推荐答案
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");
// ...
另请参见:
- 在任何与Servlet相关的类中按名称获取JSF托管bean
- Get JSF managed bean by name in any Servlet related class
See also:
无关与具体问题无关,这可能不是最好的方法.如果可能,请查看 @ManagedProperty
.
Unrelated to the concrete problem, this may not be the best way. Look at @ManagedProperty
if possible.
这篇关于如何从视图范围获取豆类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!