如何检查用@WindowScoped存储的对象是否正确存储? [英] How can I check if an object stored with @WindowScoped is stored correctly?

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

问题描述

两天前,我写了这个问题: 如何在@WindowScoped上检索对象? BalusC提出了一些建议,现在我有一些问题需要了解,如果我的问题是WindowScoped中的对象是否正确存储或我的代码检索错误了!

好吧,正如我所说,我有一个对象存储在@WindowScoped批注中,但是我只能在第一次时检索该对象!为什么?

我只是有一个疑问:MyFaces的CODI扩展可以以某种方式配置吗?或者我可以简单地将jar文件添加到我的项目中来使用它?

但是,这些是我的代码的一部分,因为我不知道问题出在哪里:

LogicBean.java(我应该检索的对象):

@ManagedBean (name="logicBean" )
@WindowScoped
public class LogicBean implements Serializable 
{
    String pageIncluded;
    // getter and setter methods

    public String action(String value)
    {
        setPageIncluded(value);

        return "include";
    }
}

include.xhtml:

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

ProgettiController.java

@ManagedBean(name = "progettiController")
@SessionScoped
public class ProgettiController implements Serializable {

    private FacesContext context = FacesContext.getCurrentInstance();
    private LogicBean logicBean = context.getApplication().evaluateExpressionGet(context, "#{logicBean}", LogicBean.class);
    //getter and setter methods

    public void testMethod()
    {
        logicBean.action("WEB-INF/jsf/page1.xhtml");
    }
}

我也尝试使用@ManagedProperty(#{logicBean}")并将范围设置为WindowScoped,但没有任何变化...


经过一些新的试验,我发现一个奇怪的问题,在我的include.xhtml上,我添加了#{progettiController.logicBean.getPageIncluded()}和#{logicBean.getPageIncluded()}来检查这两个字段?

好吧,当我第一次加载应用程序时,正确设置了变量并看到了我想要的东西,第二次,第一个变量设置了新值,但是第二个为空,什么也看不到,但现在来了奇怪的事情...如果我应该再次尝试该应用程序,则应打开index.xhtml,其中包含以下形式:

<h:form>
    <h:commandLink action="#{logicBean.action('/WEB-INF/jsf/progetti/List.xhtml')}" value="Show All Progetti Items"/>
</h:form>

是什么结果? 第一个变量仍设置为旧值(错误),但第二个变量设置正确,因此我可以像往常一样查看页面! 如果有人可以帮助我,我将永远感谢他/她!

解决方案

CODI是CDI的扩展,因此您应该通过CDI @Named批注而不是JSF @ManagedBean批注来管理bean.然后,您可以通过CDI @Inject注释注入另一个bean.以下示例应该起作用:

import javax.inject.Named;
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowScoped;

@Named
@WindowScoped
public class LogicBean implements Serializable {
    // ...
}

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@SessionScoped
public class ProgettiController implements Serializable {

    @Inject
    private LogicBean logicBean;

    // ...
}

Two days ago I wrote this question: How can I retrieve an object on @WindowScoped? and BalusC answered with some suggestions, now I have some problem to understand if my problem is that the object in WindowScoped is stored properly or my code to retrieve it is wrong!

Well, as I said, I have an object that I stored in @WindowScoped annotation but I can retrive this object only the first time! Why?

I just have a doubt: the CODI extension of MyFaces could be configured in some manner? Or I can use it simple adding the jar files to my project?

However, these are parts of my code because I don't know where is the problem:

LogicBean.java (the object that I should retrive):

@ManagedBean (name="logicBean" )
@WindowScoped
public class LogicBean implements Serializable 
{
    String pageIncluded;
    // getter and setter methods

    public String action(String value)
    {
        setPageIncluded(value);

        return "include";
    }
}

include.xhtml:

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

ProgettiController.java

@ManagedBean(name = "progettiController")
@SessionScoped
public class ProgettiController implements Serializable {

    private FacesContext context = FacesContext.getCurrentInstance();
    private LogicBean logicBean = context.getApplication().evaluateExpressionGet(context, "#{logicBean}", LogicBean.class);
    //getter and setter methods

    public void testMethod()
    {
        logicBean.action("WEB-INF/jsf/page1.xhtml");
    }
}

I tried also using @ManagedProperty("#{logicBean}") and setting the scope as WindowScoped but nothing change...


EDIT: after some new trials I found a strange problem, on my include.xhtml I added #{progettiController.logicBean.getPageIncluded()} and #{logicBean.getPageIncluded()} for check these two fields o?

Well, when I load the application for the first time the variables are correctly set and I see what I want, the second time the first variable is setted with the new value but the second is empty and I don't see anything, but now is coming the strange thing... if I should try again the app I should open index.xhtml where I had some forms like this:

<h:form>
    <h:commandLink action="#{logicBean.action('/WEB-INF/jsf/progetti/List.xhtml')}" value="Show All Progetti Items"/>
</h:form>

and which is the result? The first variable remains set with the old value (wrong) but the second is setted correctly so I can review the page like I would! If someone can help me I will thank him/her forever!

解决方案

CODI is an extension to CDI, so you should manage your beans by CDI @Named annotation instead of the JSF @ManagedBean annotation. Then you can inject the other bean by CDI @Inject annotation. The following example should work:

import javax.inject.Named;
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowScoped;

@Named
@WindowScoped
public class LogicBean implements Serializable {
    // ...
}

and

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@SessionScoped
public class ProgettiController implements Serializable {

    @Inject
    private LogicBean logicBean;

    // ...
}

这篇关于如何检查用@WindowScoped存储的对象是否正确存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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