当从我的门户网站的登录页面导航到索引页面时,根据某些事实可以将用户重定向到外部,这看起来像:
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)
请您给我一个建议,以避免发生此异常?请注意重定向已完成,但由于此异常,当我回到我的门户网站时,它已不再正常工作...
答案 0 :(得分:2)
您应该使用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()
,您不需要自己执行此操作。
此外,您需要确保您没有在同一响应中多次调用重定向方法,或者之后正在执行导航。