如何检查使用 @WindowScoped 存储的对象是否正确存储?

发布于 2024-12-27 19:08:28 字数 1952 浏览 1 评论 0原文

前两天我写了这个问题: 如何检索 @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>

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2025-01-03 19:08:28

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;

    // ...
}

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;

    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文