1
votes

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

1 Answers

0
votes

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");
}