首先获得HttpServletResponse
import javax.servlet.http.*;
protected
HttpServletResponse getResponse()
...
{
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) facesContext
.getExternalContext().getResponse();
return response;
}
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();
}