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

查看:127
本文介绍了@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
          }
        }
        ....
     }

这对于我的两个Backing Bean来说效果很好...但是对于其他两个,non-redirected-xhtml文件仍在调用Backing Bean,并且不会重定向.我已经(通过调试)确认了支持Bean确实调用了FacesContext.getCurrentInstance().getExternalContext().redirect("bolagsSok_company.xhtml");并返回了;陈述.

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.

使用<f:event type="preRenderView">代替@PostConstruct.这将在渲染响应开始之前(因此,在将任何位发送到响应之前)调用该方法.或者,如果您已经在使用JSF 2.2,请使用<f:viewAction>.另一个优点是<f:viewAction>可以返回类似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天全站免登陆