问题
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