【问题标题】:Java JSF redirect on logout注销时 Java JSF 重定向
【发布时间】:2013-06-06 09:48:01
【问题描述】:

我有一个 ViewScoped JSF 页面。用户正在工作的地方存在一个ace:dialog。如果用户两个小时没有点击,他的会话就会被tomcat自动销毁。

如果用户在两个小时后发送请求,我会重定向到登录页面(因为用户已注销)。问题是我变成了一个错误:

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

如果用户退出,有没有办法将每个请求重定向到登录页面? 如果会话被破坏,我的 Backing Beans 会发生什么?

如果用户请求子站点但未登录,这就是我的重定向方式:

@PostConstruct
public void initialize() {

    logger.debug("Start - " + new Throwable().getStackTrace()[0]);

    if (hasReadAccess()) {
        FacesContext.getCurrentInstance().getExternalContext().redirect(pPath);
        return;
    }

    logger.debug("End- " + new Throwable().getStackTrace()[0]);
}

这就是我的代码的方式,如果用户发送 ajax 请求,例如使用 rowEditListener:

public void rowEditListener(RowEditEvent ev) {

logger.debug("Start - " + new Throwable().getStackTrace()[0]);

if (hasReadAccess()) {
    FacesContext.getCurrentInstance().getExternalContext().redirect(pPath);
    return;
}

// do something

logger.debug("End - " + new Throwable().getStackTrace()[0]);
}

谢谢!

【问题讨论】:

  • 只是一个旁注。仅仅为了获取当前方法的名称而创建一个异常太昂贵了,最好使用Thread.currentThread().getStackTrace()[1].getMethodName()
  • 感谢异常提示和链接。我检查它。

标签: java jsf-2


【解决方案1】:
you can use spring:
public void logout() {
        try {
            SecurityContextHolder.getContext().setAuthentication(null);
            FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/j_spring_security_logout?faces-redirect=true");

        } catch (Exception e) {

        }
    }

【讨论】:

    猜你喜欢
    • 2012-07-16
    • 2017-06-01
    • 2018-12-04
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    相关资源
    最近更新 更多