JSF 2.0 redirect error

对着背影说爱祢 提交于 2019-12-23 02:34:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!