JSF2.0 中会话如何设置和取消设置
我想了解如何在 JSF2.0 中设置和取消设置会话。虽然关注了一些博客和书籍(Core JavaServer Faces-3rd Edition),但我知道使用注释@SessionScoped我们可以将任何管理bean设置为会话。我有一个 loginBean,它是 @ManagedBean 和 SessionScoped 声明的。在我的网站的右上角,有登录按钮。 当创建此会话时(我没有手动设置它,这就是我感到困惑的原因)以及当我被销毁时?它必须通过超时或仅通过单击注销按钮来销毁。
I want to know about setting and un-setting the session in JSF2.0. Although following some blogs and books (Core JavaServer Faces-3rd Edition), i got to know that using annotation @SessionScoped we can set any manage bean to be in session. I have a loginBean which is @ManagedBean and SessionScoped declared. On the top right corner, my web has login button.
When this session is created (i am not setting it manually, that is why i am confused) and when i gets destroyed? It must be destroyed either by time out or by clicking in logout button only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JSF 在幕后使用 Servlet API。会话范围的托管 bean 本质上是设置为
HttpSession
。每当第一次计算引用托管 bean#{sessionBean}
的 EL 表达式时,都会创建并设置它。每当会话过期(由于客户端重新启动或服务器超时)或失效时,它将从会话中“删除”。如果您让注销按钮调用ExternalContext#invalidateSession()
,则会话将失效。如果您熟悉基本的 Servlet API,您应该已经了解这一切是如何工作的。要深入了解 Servlet 的
HttpSession
在 JSF 的支持下工作,请阅读以下答案:Servlet 如何工作?实例化、会话、共享变量和多线程。JSF uses the Servlet API under the covers. A session scoped managed bean is in essence set as an attribute of the
HttpSession
. It will be created and set whenever the EL expression referencing the managed bean#{sessionBean}
is evaluated for the first time. It will be "removed" from the session whenever the session expires (by either a restart of the client or a timeout in the server) or get invalidated. If you let your logout button callExternalContext#invalidateSession()
, then the session will be invalidated.If you're familiar with the basic Servlet API, you should already understand how this all works. For an in-depth explanation of the Servlet's
HttpSession
works under JSF's covers, read this answer: How do servlets work? Instantiation, sessions, shared variables and multithreading.在jsf 2.0中,我们可以将总类ob设置为会话,就像我提到的
Class_name sm一样;
ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext(); extContext.getSessionMap().put("给出访问该属性的名称",sm);
Class_name sm = (Class_name) extContext.getSessionMap().get("给出访问此属性的名称");
In jsf 2.0 we can set total class ob as session like i mention
Class_name sm;
ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext(); extContext.getSessionMap().put("Give name for access this property",sm);
Class_name sm = (Class_name) extContext.getSessionMap().get("Give name for access this property");