IllegalStateException when trying to .getSessionMap() from a Session Scoped Bean

主宰稳场 提交于 2019-12-01 13:27:48

The FacesContext is stored in the HTTP request thread. You should absolutely not declare and assign it as an instance variable of an instance which lives longer than the HTTP request (and preferably also just not when it's already request based -it's bad design). The FacesContext instance is released and invalidated when the HTTP request finishes. In any subsequent HTTP request the instance is not valid anymore. There's means of an illegal state. That explains the IllegalStateException you're seeing.

You need to remove the following line:

private FacesContext context = FacesContext.getCurrentInstance();

And fix your code to get it only threadlocal in the method block:

Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
// ...

You can always assign it as a variable, but that should only be kept threadlocal:

FacesContext context = FacesContext.getCurrentInstance();
Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
// ...

Unrelated to the concrete problem, using @ManagedProperty has been easier in this particular case.

public final class MenuBean implements Serializable {  

    @ManagedProperty("#{user}")
    private EUser user;

    // ...
}

JSF will then inject it for you.

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