注销后重定向失败并出现 java.lang.IllegalStateException:提交响应后无法创建会话
我有一个 JSF2 应用程序。我有一个会话范围的登录 bean 和一个视图范围的注销 bean。当我登录时,我使用重定向并且工作正常。但是,注销因重定向而失败。如果我注销而不重定向它就可以了。
@ManagedBean
@ViewScoped
public class MbLogout extends BaseJsf {
private static final long serialVersionUID = 2992671241358926373L;
public String logout() throws DfException {
getFacesContext().getExternalContext().invalidateSession();
//return "login?faces-redirect=true"; // fails with this
return "login";
}
}
登录页面绑定到登录 bean,所以我怀疑这可能与它有关,尽管我不明白为什么它不起作用。错误是:
java.lang.IllegalStateException: Cannot create a session after the response has been committed
我的猜测是它试图在登录页面上创建一个会话,因为我访问了会话 bean,尽管我没有看到这有什么问题,并且它无需重定向即可工作。
我正在使用 MyFaces 2.1。
I have a JSF2 application. I have a login bean which is session scoped and a logout bean which is view scoped. When I login I use redirect and it works fine. However the logout fails with redirect. If I logout without redirect it works.
@ManagedBean
@ViewScoped
public class MbLogout extends BaseJsf {
private static final long serialVersionUID = 2992671241358926373L;
public String logout() throws DfException {
getFacesContext().getExternalContext().invalidateSession();
//return "login?faces-redirect=true"; // fails with this
return "login";
}
}
The login page has bindings to the login bean so I suspect this may have something to do with it, although I don't see why it doesn't work. The error is:
java.lang.IllegalStateException: Cannot create a session after the response has been committed
My guess is it's trying to create a session on the login page since I access the session bean although I don't see anything wrong with this and it works without redirect.
I'm using MyFaces 2.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用 Servlet 而不是 Bean 进行注销,托管 Bean(尤其是视图范围)不适合注销的目的。例如:
I would recommend using a Servlet rather than a bean for logout, a managed bean (especially view scoped) is not fitting for the purpose of logging out. For example:
它似乎与视图范围内的 bean 有关,该 bean 本身应该在会话中序列化。改为将其设置为请求范围。无论如何,视图范围对于注销来说没有多大意义。
It seems to be related to the bean being in the view scope which should by itself be serialized in the session. Make it request scoped instead. The view scope doesn't make much sense for logout anyway.