【发布时间】:2012-05-15 14:52:11
【问题描述】:
我需要访问应用程序范围的托管 bean 以从 HttpSessionListener 中修改某些属性。
我已经使用了类似以下的东西:
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
User user = userService.findBySessionId(session.getId());
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
ApplicationScopedBean appBean = (ApplicationScopedBean) externalContext.getApplicationMap().get("appBean");
appBean.getConnectedUsers().remove(user);
}
externalContext = FacesContext.getCurrentInstance().getExternalContext() 已经在这里导致了一个空指针异常,即使它没有,我也不确定 appBean 是否可以通过上述方式访问。
有什么想法吗?
【问题讨论】:
-
可能您的页面没有使用 FacesServlet。显示您的 web.xml JSF servlet 配置和映射以及页面的全名。
-
似乎无法从 HttpSessionListener 中使用 FacesContext.getCurrentInstance() 获取 FacesContext;就像从 Servlet 无法做到这一点一样。也许如果你能得到 ServletContext 你可以做类似 getServletContext().getAttribute("appBean");
标签: java jsf jakarta-ee jsf-2