@PostConstruct 中的重定向会导致 IllegalStateException [英] Redirect in @PostConstruct causes IllegalStateException

查看:28
本文介绍了@PostConstruct 中的重定向会导致 IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 4 个支持 bean 中的 @PostConstruct 中进行重定向.正如我从以下问题中了解到的:JSF PostConstruct 异常处理 - 重定向我知道我应该使用:

I want to make a redirect in my @PostConstruct in 4 of my backing beans. As I've learned from the follwoing question: JSF PostConstruct Exception Handling - Redirect I know that I'm supposed to use:

    @PostConstruct
    public void init() {    
       if (shouldRedirect) {
          try { 
             FacesContext.getCurrentInstance().getExternalContext().redirect("bolagsSok_company.xhtml");
             return;
          } catch (IOException e) {
             //do nothing
          }
        }
        ....
     }

这对我的 2 个支持 bean 非常有用……但对于另外两个,非重定向-xhtml 文件仍在调用支持 bean 并且不重定向.我已经确认(通过调试)支持 bean 确实调用了 FacesContext.getCurrentInstance().getExternalContext().redirect("bolagsSok_company.xhtml"); 和 return;声明.

This works great for 2 of my Backing beans... but for the other two, the non-redirected-xhtml file is still making calls to the backing bean and doesn't redirect. I've confirmed (with debug) that the backing beans indeed calls both FacesContext.getCurrentInstance().getExternalContext().redirect("bolagsSok_company.xhtml"); and return; statements.

有什么线索可能出问题了吗?

Any clues what could be wrong?

推荐答案

如果响应已经提交,在 @PostConstruct 中重定向可能为时已晚.IE.当响应的前几个字节已经发送到客户端时.这是一个不归路.在您的情况下,当第一次在视图中相对较晚地引用(并因此构造)支持 bean 时,可能会发生这种情况,可能大约在一半或最后.

Redirecting in a @PostConstruct might be too late if the response is already committed. I.e. when the first few bytes of the response are already been sent to the client. This is a point of no return. That can in your case happen when the backing bean is referenced (and thus constructed) for the first time relatively late in the view, maybe about halfway or in the end.

您可以通过以下方式之一解决此问题:

You could solve this in one of the following ways:

  1. 尽可能早地在视图中第一次引用 bean.

  1. Reference the bean for the first time as early as possible in the view.

使用 而不是 @PostConstruct.这将在渲染响应开始之前调用该方法(因此,在任何位被发送到响应之前).或者,当您已经使用 JSF 2.2 时,使用 .另一个优点是 可以返回导航案例结果,如 return bolagsSok_company?faces-redirect=true" 而无需摆弄 ExternalContext#redirect().

Use <f:event type="preRenderView"> instead of @PostConstruct. This will invoke the method right before the render response starts (thus, before any bit is been sent to the response). Or, when you're on JSF 2.2 already, use the <f:viewAction>. Additional advantage is that the <f:viewAction> can return a navigation case outcome like return bolagsSok_company?faces-redirect=true" without the need to fiddle with ExternalContext#redirect().

通过 web.xml 中的 javax.faces.FACELETS_BUFFER_SIZE 上下文参数将默认 Facelets 缓冲区大小增加到最大 HTML 响应的大小.

Increase the default Facelets buffer size by javax.faces.FACELETS_BUFFER_SIZE context param in web.xml to about the size of the largest HTML response.

另见:

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆