繁体   English   中英

JSF 重定向异常

[英]JSF redirect exception

从我的门户的登录页面导航到索引页面时,有一种情况是基于某些事实,用户可以从外部重定向,如下所示:

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)

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

您应该使用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() ,您无需自己执行。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM