简体   繁体   English

JSF SessionScopedmanagedBean与ViewScopedmanagedBean结合使用

[英]JSF SessionScoped managedBean in conjunction with ViewScoped managedBean

I have the following problem in my project: 我的项目中存在以下问题:

We have 2 managed beans with the following configuration: 我们有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());
    }
}

This is not all of the code, but it gives you a general idea of the sitatution: 这不是全部代码,但是它为您提供了大致的概念:

  • SessionScoped bean which holds a session object 具有会话对象的SessionScoped bean
  • ViewScoped bean which holds view-related objects and a reference to the SessionScoped bean ViewScoped bean,其中包含与视图相关的对象以及对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 在某个时刻,我使用来自视图的信息在SessionScoped bean中设置了会话对象,但我离开了视图...到目前为止一切都很好
  • 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) 现在,当我返回到该视图时,他创建了一个新的ViewScoped bean实例(如预期的那样)并设置了SessionScoped bean(由于ManagedProperty批注)
  • When I debug this, I can see that the reference to the SessionScoped bean remains the same, so it's the same object 当我调试它时,我可以看到对SessionScoped bean的引用保持不变,因此它是同一对象
  • However, the object that I previously set in the SessionScoped bean has become null , meaning he 'lost' the session information about that object 但是,我之前在SessionScoped bean中设置的对象已变为null ,这意味着他“丢失”了有关该对象的会话信息。
  • I never reset this object in the SessionScoped bean, so I don't see where it could have gone wrong... 我从未在SessionScoped bean中重置此对象,所以我看不到它可能在哪里出错了...

If you want more explanation or if you want me to phrase this question better, let me know or use the edit button! 如果您需要更多说明,或者希望我更好地表达这个问题,请告诉我或使用“编辑”按钮!

@BalusC I think we may have found the problem. @BalusC我想我们可能已经找到了问题。 In our web.xml, we were using the following setting: 在我们的web.xml中,我们使用以下设置:

 <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. 现在,如果我正确理解它,我们将无法通过具有此设置的视图范围的Bean编辑会话范围的Bean的状态...我们唯一能够更改会话范围的Bean的状态的方法,位于bean本身的构造函数或PostConstruct方法中。 Changes made to the session-scoped bean from inside the view-scoped bean were not persisted on the server. 从视图范围的Bean内部对会话范围的Bean所做的更改未持久保存在服务器上。

If we set this variable to 'server', the state is indeed persisted on the server and not transmitted to the client every time. 如果我们将此变量设置为“ server”,则状态确实会保留在服务器上,并且不会每次都传输给客户端。 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. 我知道这会给服务器增加内存方面的负担,但是我想知道如何使用client方法解决问题。

So, my problem is fixed, but I'm not sure if the solution is ideal... 因此,我的问题已解决,但是我不确定该解决方案是否理想...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM