问题描述
我有一个会话范围的CDI托管bean:
I have a session scoped CDI managed bean:
@Named
@SessionScoped
public class SampleBean implements Serializable {
// ...
}
我需要在一定流程之后从会话中删除此bean,为此我使用了以下代码,如:
I need to remove this bean from the session after a certain flow for which I used the following code like as seen in this answer:
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.getSessionMap().remove("sampleBean");
但是,它不起作用,并且SampleBean
保留在会话中.
我想念什么吗?
However, it does not work and the SampleBean
remains in the session.
Am I missing something?
推荐答案
与JSF托管Bean相反,CDI托管Bean并非直接通过其托管Bean名称直接存储在会话映射中.相反,它们由CDI管理器实现(例如Weld,OpenWebBeans等)存储在服务器的内存中,例如会话ID作为键.
In contrary to JSF managed beans, CDI managed beans are not stored directly by their managed bean name in the session map. They are instead stored in server's memory by the CDI manager implementation (Weld, OpenWebBeans, etc) using e.g. session ID as key.
因此,您在此处使用的技巧不适用于CDI管理的bean.您需要寻找一种替代方法.在这种情况下,正确的方法是使用@ConversationScoped
而不是@SessionScoped
.在设计正确的Web应用程序中,永远不需要手动终止作用域.因此,首先使用@SessionScoped
进行对话/流程是错误的.
The trick which you used there is therefore not applicable on CDI managed beans. You need to look for an alternate approach. The right approach in this particular case is to use @ConversationScoped
instead of @SessionScoped
. In properly designed web applications, there should never be the need to manually terminate a scope. So using @SessionScoped
for a conversation/flow was already wrong in first place.
这篇关于删除/销毁会话范围的CDI托管bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!