问题
I get an IllegalStateException when redirecting from a preRenderView event. I have worked around it by just ingoring the exception. Is there a cleaner way to achieve the same result?
@Named
@RequestScoped
public class LogoutBean implements Serializable
{
public void preRenderView(ComponentSystemEvent e)
{
userSessionBean.logout();
FacesContext ctx = FacesContext.getCurrentInstance();
try
{
ctx.getApplication().getNavigationHandler().handleNavigation(ctx, null, "/pages/index?faces-redirect=true");
}
catch (IllegalStateException exc)
{
// Ignore. This exception is caused by redirecting after the response is already committed. The redirect works anyway.
}
}
@Inject
private UserSessionBean userSessionBean;
}
回答1:
I'd suggest to send a redirect by ExternalContext#redirect() instead.
public void preRenderView(ComponentSystemEvent e) throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect("pages/index.xhtml");
}
来源:https://stackoverflow.com/questions/6526251/jsf-2-0-redirect-error