序列化FacesContext或服务器重新启动后如何获取属性值 [英] Serialize FacesContext or How to get properties-values after Server Restart

查看:105
本文介绍了序列化FacesContext或服务器重新启动后如何获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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).

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

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

推荐答案

您的问题描述不清楚,但是症状表明您试图在任何地方获取FacesContext的当前实例作为实例变量.您根本不应该这样做,您应该始终在本地方法范围内获取当前实例.

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.

因此,您应该从不进行操作

private FacesContext context;

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

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

但是您应该这样做:

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

否则,如果您使用的是会话范围的Bean,则仅保存创建该Bean的第一个HTTP请求的实例.确切地说,此HTTP请求在关联的响应结束时已被丢弃,并且在任何后续请求中均不再有效.因此,绝对不应该将FacesContext序列化.

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.

这篇关于序列化FacesContext或服务器重新启动后如何获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆