如何在@WindowScoped上检索对象? [英] How can I retrieve an object on @WindowScoped?

查看:99
本文介绍了如何在@WindowScoped上检索对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这篇文章中动态ui:include 我问我如何以某种允许状态存储对象我要加载同一浏览器的新窗口或选项卡,但它也没有存储在新窗口中. Adrian Mitev告诉我使用@WindowScoped,这是MyFaces扩展的一个选项,称为CODI,我尝试实现它.

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.

现在我应该说我是盲人,当我尝试打开Apache Wiki时,我的浏览器在许多页面上崩溃,因此我无法阅读这些指南.

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.

但是,我在项目中添加了源代码,并且编译器未给出任何错误. 问题是,现在当我尝试检索由@WindowScoped存储的bean时,该页面无法正常工作!

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!

我在我的bean中使用以下代码:

I use this code in my bean:

@ManagedBean (name="logicBean" )
@WindowScoped

include.xhtml中,我使用以下代码检索参数:

In include.xhtml I retrieve the parameter with this code:

<ui:include src="#{logicBean.pageIncluded}"/> 

在我的其他bean中,我使用此代码检索LogicBean(并且我确定问题出在此代码上)

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");

如何检索正确的" LogicBean对象?

How can I retrive the "correct" LogicBean object?

推荐答案

您正在尝试从会话映射中获取LoginBean.这仅适用于具有标准JSF @SessionScoped批注的会话范围的Bean.

You're trying to get the LoginBean from the session map. This works only for session scoped beans with the standard JSF @SessionScoped annotation.

访问其他bean的规范方法是在检索bean上使用@ManagedProperty.

The canonical way to access other beans is using @ManagedProperty on the retrieving bean.

例如

@ManagedBean
@RequestScoped
public class OtherBean {

    @ManagedProperty("#{logicBean}")
    private LogicBean logicBean;

    // Getter+Setter.
}

如果确实需要通过编程评估EL在方法块内访问它,则应使用

If you really need to access it inside the method block by evaluating the EL programmatically, you should be using Application#evaluateExpressionGet() instead:

FacesContext context = FacesContext.getCurrentInstance();
LogicBean logicBean = context.getApplication().evaluateExpressionGet(context, "#{logicBean}", LogicBean.class);
// ...

这篇关于如何在@WindowScoped上检索对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆