JSF SessionScoped ManagedBean 与 ViewScoped ManagedBean 结合使用

发布于 2024-12-25 20:43:54 字数 1091 浏览 1 评论 0原文

我的项目中遇到以下问题:

我们有 2 个具有以下配置的托管 bean:

@ManagedBean
@SessionScoped
public class SessionBean {
    private PersonnelFile personnelFile; // + getters/setters
}

@ManagedBean
@ViewScoped
public class ViewBean {
    @ManagedProperty("#{sessionBean}")
    private SessionBean sessionBean;

    public void selectPersonnel() {
        sessionBean.getPersonnelFile().setPerson(new Person());
    }
}

这不是全部代码,但它使您对情况有一个大概的了解:

  • SessionScoped bean 持有会话对象
  • ViewScoped bean 持有视图相关对象和对 SessionScoped bean 的引用

现在的问题是:

  • 我访问我的视图
  • 我对同一个视图进行了几次回发,每次都回到该视图..到目前为止一切都很好
  • 在某个时刻,我设置我的SessionScoped bean 中的会话对象包含视图中的信息,然后我离开我的视图...到目前为止一切顺利
  • 现在,当我返回到此视图时,他创建一个新的 ViewScoped bean 实例(如预期)并将 SessionScoped bean 设置回来(因为有 ManagedProperty 注释)
  • 当我调试这个时,我可以看到对 SessionScoped bean 的引用保持不变,因此它是同一个对象
  • 但是,我之前在 SessionScoped bean 中设置的对象已变成null,这意味着他“丢失”了有关该对象的会话信息
  • 我从未在 SessionScoped bean 中重置该对象,因此我不知道它可能在哪里出错...

如果您需要更多解释或者如果您希望我更好地表达这个问题,请告诉我或使用编辑按钮!

I have the following problem in my project:

We have 2 managed beans with the following configuration:

@ManagedBean
@SessionScoped
public class SessionBean {
    private PersonnelFile personnelFile; // + getters/setters
}

@ManagedBean
@ViewScoped
public class ViewBean {
    @ManagedProperty("#{sessionBean}")
    private SessionBean sessionBean;

    public void selectPersonnel() {
        sessionBean.getPersonnelFile().setPerson(new Person());
    }
}

This is not all of the code, but it gives you a general idea of the sitatution:

  • SessionScoped bean which holds a session object
  • ViewScoped bean which holds view-related objects and a reference to the SessionScoped bean

The problem now is:

  • I access my view
  • I make a few post-backs to the same view, coming back to the view every time.. so far so good
  • At a certain moment, I set my session object in the SessionScoped bean with the information from the view and I leave my view... so far so good
  • Now, when I return to this view, he creates a new ViewScoped bean instance (as expected) and sets the SessionScoped bean back (because of the ManagedProperty annotation)
  • When I debug this, I can see that the reference to the SessionScoped bean remains the same, so it's the same object
  • However, the object that I previously set in the SessionScoped bean has become null, meaning he 'lost' the session information about that object
  • I never reset this object in the SessionScoped bean, so I don't see where it could have gone wrong...

If you want more explanation or if you want me to phrase this question better, let me know or use the edit button!

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

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

发布评论

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

评论(1

快乐很简单 2025-01-01 20:43:54

@BalusC
我想我们可能已经发现问题了。在我们的 web.xml 中,我们使用了以下设置:

 <context-param>
     <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
     <param-value>client</param-value>
 </context-param>

现在,如果我理解正确的话,我们无法使用此设置通过视图范围 bean 来编辑会话范围 bean 的状态...我们唯一的方法是已经能够更改会话范围 bean 的状态,可以在 bean 本身的构造函数或 PostConstruct 方法中进行。从视图范围 bean 内部对会话范围 bean 所做的更改不会保留在服务器上。

如果我们将此变量设置为“server”,则状态确实会保存在服务器上,而不是每次都传输到客户端。我知道这会给服务器带来更多的内存负载,但我想知道如何通过使用客户端方法来解决我们的问题。

所以,我的问题已经解决,但我不确定解决方案是否理想......

@BalusC
I think we may have found the problem. In our web.xml, we were using the following setting:

 <context-param>
     <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
     <param-value>client</param-value>
 </context-param>

Now, if I understand it correctly, we can't edit the state of a session-scoped bean through a view-scoped bean with this setting... The only way we have been able to change the state of the session-scoped bean, is in the constructor or PostConstruct method of the bean itself. Changes made to the session-scoped bean from inside the view-scoped bean were not persisted on the server.

If we set this variable to 'server', the state is indeed persisted on the server and not transmitted to the client every time. I understand this puts more load on the server in terms of memory, but I would like to know how we could solve our problem by using the client approach.

So, my problem is fixed, but I'm not sure if the solution is ideal...

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