【问题标题】:JSF redirect exceptionJSF 重定向异常
【发布时间】:2012-04-06 11:30:26
【问题描述】:

当从我的门户的登录页面导航到索引页面时,根据某些事实,用户可以被外部重定向,如下所示:

if (!(marketVo.getAbsoluteUrl() != null && marketVo.getAbsoluteUrl().equals(absoluteUrlToRedirect))) {
logger.info("---WILL REDIRECT TO ABS URL: " + absoluteUrlToRedirect);
final FacesContext context = FacesContext.getCurrentInstance();
context.responseComplete();
try {
    final HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

    if (context.getViewRoot() != null) {
        // this step will clear any queued events
        context.getViewRoot().processDecodes(context);
    }
    response.sendRedirect(absoluteUrlToRedirect);

} catch (final Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

好吧,它抛出了一个异常:

 14:24:35,579 INFO  [CmwSessionHelperBean] ---WILL REDIRECT TO ABS URL: http://hi
tachi.mygravitant.com
14:24:35,580 ERROR [STDERR] java.lang.IllegalStateException
14:24:35,582 ERROR [STDERR]     at org.apache.catalina.connector.ResponseFacade.
sendRedirect(ResponseFacade.java:435)
14:24:35,590 ERROR [STDERR]     at com.example.cloud.common.jsf.core.beans.Cmw
SessionHelperBean.createCmwUserSession(CmwSessionHelperBean.java:269)

您能否给我一个建议以避免发生此异常?请注意,重定向已完成,但由于此异常,当我返回门户时,它不再正常工作...

【问题讨论】:

    标签: java jsf jsf-1.2


    【解决方案1】:

    您应该使用 ExternalContext#redirect() 以 JSF 安全的方式执行重定向。

    public void createCmwUserSession() throws IOException {
    
        if (!(marketVo.getAbsoluteUrl() != null && marketVo.getAbsoluteUrl().equals(absoluteUrlToRedirect))) {
            logger.info("---WILL REDIRECT TO ABS URL: " + absoluteUrlToRedirect);
            FacesContext.getCurrentInstance().getExternalContext().redirect(absoluteUrlToRedirect);
        }
    }
    

    这个方法也会隐式调用FacesContext#responseComplete(),不用你自己做。

    您还需要确保您没有在同一个响应上多次调用重定向方法,或者在之后执行导航。

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 2014-08-11
      相关资源
      最近更新 更多