0

问题如下:我需要序列化用户会话,因此,在服务器重新启动后它仍然存在。

使用 JavaEE 和 Tomcat 7 可以正常工作implements Serializable,但问题是FacesContext. 确实,在重新启动服务器后,FacesContext.getCurrentInstance()返回null,因此我无法访问消息包(因此我message.properties再也找不到了)。

FacesContext那么,重启Tomcat时如何保留?

4

1 回答 1

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.

于 2012-10-01T11:29:54.113 回答