首先获得HttpServletResponse

protected HttpServletResponse getResponse() ...{
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) facesContext
.getExternalContext().getResponse();
return response;
}方法a: getResponse().sendRedirect(link);
可以重定向,但会提示错误:
java.lang.IllegalStateException: Cannot forward after response has been committed
方法b:
getResponse().setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
getResponse().setHeader("Location",link);
方法c:
public static void redirectPage(String szPage)
{
FacesContext context = FacesContext.getCurrentInstance();
javax.faces.application.Application app = context.getApplication();
UIViewRoot view = app.getViewHandler().createView(context, szPage);
context.setViewRoot(view);
context.renderResponse();
}
本文介绍了在JavaServer Faces (JSF) 中实现页面重定向的三种不同方式:使用HttpServletResponse进行重定向、设置状态码及头部信息进行永久重定向、通过创建新的视图根并渲染响应来实现内部重定向。
803

被折叠的 条评论
为什么被折叠?



