2013-06-06 79 views
0

我有一个ViewScoped JSF页面。在用户正在工作的地方存在一个ace:对话框。如果用户没有点击两个小时,他的会话将被tomcat自动销毁。退出时的Java JSF重定向

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

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

有没有办法每个请求登录页面重定向如果用户成为注销? 如果会话被破坏,我的后盾豆怎么了?

那我的重定向的方式,如果用户请求一个亚位点和在未登录:

@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]); 
} 

Thanks!

+1

参见:http://stackoverflow.com/questions/11203195/session-timeout-and-viewexpiredexception-handling-on-jsf-primefaces-ajax-request – zargarf

+1

只是一个方面说明。创建一个Exception只是为了获取当前方法的名字太昂贵了,最好使用'Thread.currentThread()。getStackTrace()[1] .getMethodName()' – A4L

+0

感谢您的提示与异常和链接。我检查它。 – Nils

回答

-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) { 

     } 
    }