【问题标题】:What's the convenient way to destroy @SessionScoped?销毁@SessionScoped 的便捷方法是什么?
【发布时间】:2013-10-21 02:30:58
【问题描述】:

我的问题是数据表中的数据没有刷新。当我单击菜单中的项目时,我想销毁会话范围。我知道 Viewscoped 可以,但我想学习其他方式。 提前致谢。

控制器:

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

菜单:

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

【问题讨论】:

  • 把你的bean改成@ViewScoped
  • 换句话说,您根本不希望它是会话范围的。
  • 我真的想保留会话范围,我只想从菜单中终止会话范围。

标签: jsf jsf-2


【解决方案1】:

没有真正“干净”的方式来做到这一点。 @SessionScoped bean 应该和 Session 一样长。因此我再次强调,你应该更好地调整 beans 范围。

但如果你真的还需要这样做,最简单的方法就是这样做:

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

对于@ViewScoped bean,您可以这样做:

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

【讨论】:

  • 谢谢,我必须使用 removeSessionScopedBean("MyController");
  • @user2683519 如果对您有用,请将其标记为已接受。然后它将不再被列为未答复。
猜你喜欢
  • 1970-01-01
  • 2015-10-28
  • 2012-05-16
  • 1970-01-01
  • 2020-09-14
  • 2011-07-03
  • 2015-05-19
  • 2011-05-26
  • 2013-09-02
相关资源
最近更新 更多