JSF 2.0重定向错误

时间:2011-06-29 19:39:19

标签: jsf-2

IllegalStateException事件重定向时,我得到preRenderView。我只是通过插入异常来解决这个问题。是否有更简洁的方法来实现相同的结果?

@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 个答案:

答案 0 :(得分:0)

我建议您通过ExternalContext#redirect()发送重定向。

public void preRenderView(ComponentSystemEvent e) throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().redirect("pages/index.xhtml");
}