从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;
}
答案 0 :(得分:0)
我建议您通过ExternalContext#redirect()
发送重定向。
public void preRenderView(ComponentSystemEvent e) throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect("pages/index.xhtml");
}