如何检索 @WindowScoped 上的对象?
在这篇文章 Dynamic ui:include 中,我问如何以某种状态存储一个对象,以便允许我加载同一浏览器的新窗口或选项卡,并且它也没有存储在新窗口中。 Adrian Mitev 告诉我使用 @WindowScoped,这是 MyFaces 扩展的一个选项,称为 CODI,我尝试实现它。
现在我应该说我是盲目的,当我尝试打开 Apache Wiki 时,我的浏览器在许多页面上崩溃,所以我无法阅读指南。
但是,我在项目中添加了源代码,编译器没有给出任何错误。 问题是,现在当我尝试检索由 @WindowScoped
存储的 bean 时,页面无法正常工作!
我在我的 bean 中使用此代码:
@ManagedBean (name="logicBean" )
@WindowScoped
在 include.xhtml
中,我使用此代码检索参数:
<ui:include src="#{logicBean.pageIncluded}"/>
在我的其他 bean 中,我使用此代码检索 LogicBean
(并且我'我确信问题出在这段代码上)
LogicBean l = (LogicBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("logicBean");
我怎样才能检索“正确的”LogicBean 对象?
In this post Dynamic ui:include I asked how I could store an object in some state that could permit me to load a new windows, or tab, of the same browser and it was not stored also in the new windows. Adrian Mitev told me to use @WindowScoped
, an option of MyFaces extension called CODI and i tried to implement it.
Now I should say that I'm blind and when I tried to open Apache Wiki my browser crashes on many pages so I can't read the guides.
However I add the source code on my project and the compiler didn't give any errors.
The problem is that now thepage when I try to retrive the bean that I stored by @WindowScoped
doesn't work properly!
I use this code in my bean:
@ManagedBean (name="logicBean" )
@WindowScoped
In include.xhtml
I retrieve the parameter with this code:
<ui:include src="#{logicBean.pageIncluded}"/>
And in my other beans I retrieve the LogicBean
with this code (and I'm sure that the problem is on this code)
LogicBean l = (LogicBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("logicBean");
How can I retrive the "correct" LogicBean object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试从会话映射中获取
LoginBean
。这仅适用于具有标准 JSF@SessionScoped
注释的会话作用域 bean。访问其他 bean 的规范方法是在检索 bean 上使用
@ManagedProperty
。例如,
如果您确实需要通过以编程方式评估 EL 来在方法块内访问它,则应该使用
Application#evaluateExpressionGet()
改为:You're trying to get the
LoginBean
from the session map. This works only for session scoped beans with the standard JSF@SessionScoped
annotation.The canonical way to access other beans is using
@ManagedProperty
on the retrieving bean.E.g.
If you really need to access it inside the method block by evaluating the EL programmatically, you should be using
Application#evaluateExpressionGet()
instead: