Serialize FacesContext or How to get properties-values after Server Restart

怎甘沉沦 提交于 2020-01-03 04:32:38

问题


The problem is the following: I need to serialize the user session, so, it will still be present after a server restarts.

Using JavaEE and Tomcat 7 works fine with implements Serializable, but the problem is the FacesContext. Indeed, after restarting the server, FacesContext.getCurrentInstance() returns null, and therefore I cannot access the message bundle (and therefore my message.properties cannot be found anymore).

So, how do I keep the FacesContext when restarting Tomcat?


回答1:


Your problem description is unclear, but the symptoms suggest that you're trying to get hold of the current instance of the FacesContext anywhere as an instance variable. You shouldn't do that at all, you should always get the current instance in the local method scope.

So, you should never do e.g.

private FacesContext context;

public Bean() {
    context = FacesContext.getCurrentInstance();
}

public void someMethod() {
    context...doSomething();
}

but you should instead do:

public void someMethod() {
    FacesContext.getCurrentInstance()...doSomething();
}

Otherwise you're in case of a session scoped bean only holding the instance of the very first HTTP request which created the bean. Exactly this HTTP request is garbaged by end of the associated response and not valid anymore in any subsequent requests. The FacesContext is thus definitely not supposed to be serializable.



来源:https://stackoverflow.com/questions/12265102/serialize-facescontext-or-how-to-get-properties-values-after-server-restart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!