What's the convenient way to destroy @SessionScoped?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:16:01

问题


My problem is te data is not refresh in the datatable. I want to destroy the session scoped when I clicked to the item in the menu.I know that it's possible with Viewscoped but I want to learn other way. Thank in advanced.

Controller:

@ManagedBean
@SessionScoped
public class MyController implements Serializable {
 //getters and setters
...........
 }

Menu:

 <td><h:outputLink styleClass="itemOutputLink" value="#  {request.contextPath}/pages/page.faces">Page1</h:outputLink></td>`

回答1:


There is no really "clean" way of doing that. A @SessionScoped bean should live as long as a Session. Thus I emphasize again that you should better adjust the beans scope.

But if you really still need to do it, the easiest way would be to do it like this:

public static void removeSessionScopedBean(String beanName) 
{
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(beanName);
}

For @ViewScoped beans you could do it this way:

public static void removeViewScopedBean(String beanName) 
{
    FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(beanName);
}


来源:https://stackoverflow.com/questions/19485391/whats-the-convenient-way-to-destroy-sessionscoped

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