Remove/destroy session scoped CDI managed bean

血红的双手。 提交于 2019-12-10 16:48:29

问题


I have a session scoped CDI managed bean:

@Named
@SessionScoped 
public class SampleBean implements Serializable {
    // ...
}

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");

However, it does not work and the SampleBean remains in the session.
Am I missing something?


回答1:


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.

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.




回答2:


And this ¿?

FacesContext .getCurrentInstance() .getApplication() .createValueBinding( "#{yourBeanName}").setValue(FacesContext.getCurrentInstance(), null );



来源:https://stackoverflow.com/questions/18897752/remove-destroy-session-scoped-cdi-managed-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!