如何销毁 SessionScoped bean 的对象
How to destroy the Object of a SessionScoped bean
我正在使用 SessionScoped
对象来保存网站的当前用户。例如,我想知道如何在单击按钮时在导航时销毁此对象。
请注意,我使用 CDI
进行注入,并且我注入了一个包含用户名和登录名的简单 bean 对象 User
。
一个SessionScoped
bean depends on the HTTP session. If the HTTP session is destroyed (timeout or manually session invalidation), the SessionScoped
bean的生命周期也会被破坏。
来自 SessionScoped
文档:
The session context is shared between all servlet requests that occur
in the same HTTP session. The session context is destroyed when the
HTTP session times out, after all HttpSessionListeners
have been
called, and at the very end of any request in which invalidate()
was
called, after all filters and ServletRequestListeners
have been
called.
如果您使用的是 JSF,请尝试:
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
它将使 HTTP 会话无效,并因此销毁与其关联的 bean。
如果您想在不需要使会话失效的情况下销毁对象(因为唯一应该使会话失效的按钮是注销),您可以使用另一个范围,称为会话范围,可以手动关闭.
您可以在此处找到更多信息 https://docs.jboss.org/weld/reference/latest/en-US/html/scopescontexts.html#_the_conversation_scope
我正在使用 SessionScoped
对象来保存网站的当前用户。例如,我想知道如何在单击按钮时在导航时销毁此对象。
请注意,我使用 CDI
进行注入,并且我注入了一个包含用户名和登录名的简单 bean 对象 User
。
一个SessionScoped
bean depends on the HTTP session. If the HTTP session is destroyed (timeout or manually session invalidation), the SessionScoped
bean的生命周期也会被破坏。
来自 SessionScoped
文档:
The session context is shared between all servlet requests that occur in the same HTTP session. The session context is destroyed when the HTTP session times out, after all
HttpSessionListeners
have been called, and at the very end of any request in whichinvalidate()
was called, after all filters andServletRequestListeners
have been called.
如果您使用的是 JSF,请尝试:
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
它将使 HTTP 会话无效,并因此销毁与其关联的 bean。
如果您想在不需要使会话失效的情况下销毁对象(因为唯一应该使会话失效的按钮是注销),您可以使用另一个范围,称为会话范围,可以手动关闭.
您可以在此处找到更多信息 https://docs.jboss.org/weld/reference/latest/en-US/html/scopescontexts.html#_the_conversation_scope