@PostConstruct中引发的异常导致JSF 2.1中的IllegalStateException [英] Exception thrown in @PostConstruct causes IllegalStateException in JSF 2.1

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

问题描述

我在@ViewScoped mananged bean上有一个init方法.在后构造中,我从数据库加载数据.我有一个自定义ExceptionHandlerWrapper来捕获所有异常并发送到错误页面.但是,当@PostConstuct引发异常时,我会收到IllegalStateException,并且不会重定向到错误页面.我尝试了很多组合.....

I have an init method on my @ViewScoped mananged bean. In the post construct i load data from the db. I have a custom ExceptionHandlerWrapper to catch all excptions and send to an error pages. However when @PostConstuct throws an exception i recieve an IllegalStateException and am not redirected to the error page. I have tried many combinations.....

我已经在我的ExcpetionHandler中尝试过

Ive tried this inside my ExcpetionHandler

externalContext.getRequestMap().put(ERROR_BEAN_ID, ERROR_TEXT);
externalContext.dispatch(ERROR_PAGE);
fc.responseComplete();

下面这行是我最初的内容.它也起作用

This line below is what i originally had. It also doent work

externalContext.getFlash().put(ERROR_BEAN_ID, ERROR_TEXT);
nav.handleNavigation(fc, null, ERROR_PAGE);
fc.renderResponse();

所有这些都会导致IllegalStateExceptions.我也称重定向为相同结果.

These all cause IllegalStateExceptions. I also called redirect with the same result.

您可以全局捕获@PostConstruct引发的错误吗?

Can you globally catch errors thrown from @PostConstruct?

推荐答案

所有这些都会导致IllegalStateException.

使用消息已提交响应",我认为是吗?好吧,那是无可挽回的意义.响应的一部分已经发送到客户端(Web浏览器).无法取回已发送的字节.服务器最终将在日志中出现此异常,而客户端最终将出现半生不熟的响应.

With the message "Response already committed", I assume? Well, that's a point of no return. A part of the response has already been sent to the client (the webbrowser). It's not possible to take the already sent bytes back. The server will end up with this exception in the logs and the client will end up with a halfbaked response.

你能做什么?

最直接的方法是将响应缓冲区的大小扩大到最大页面的大小.例如64KB:

Most straightforward way would be to enlarge the response buffer size to the size of the largest page. For example, 64KB:

<context-param>
    <param-name>javax.faces.FACELETS_BUFFER_SIZE</param-name>
    <param-value>65536</param-value>
</context-param>

它默认为〜2KB,具体取决于服务器配置.您只需要记住,当服务器必须处理相对大量的请求/响应时,这可能会占用大量内存.剖析并正确测量.

It defaults to ~2KB depending on the server configuration. You only need to keep in mind that this may be memory hogging when your server has to process relatively a lot of requests/responses. Profile and measure properly.

另一种方法是在之前引用该bean,然后呈现/提交响应,以便在该点之前触发它的(后)构造.可能是在视图的最底部第一次引用了所讨论的bean,远远超出了〜2KB的响应大小边界.您可以在视图顶部的某个位置接一个<f:event type="preRenderView">来接管@PostConstruct的工作.例如

Another way is to reference the bean before the response is to be rendered/committed so that it's (post)construction is triggered before that point. Perhaps the bean in question is referenced for the first time at the very bottom of the view, long beyond the ~2KB response size border. You could take over the job of @PostConstruct with a <f:event type="preRenderView"> somewhere in the top of the view. E.g.

<f:event type="preRenderView" listener="#{bean.init}" />

使用

public void init() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        // Do here your original @PostConstruct job.
    }
}

这篇关于@PostConstruct中引发的异常导致JSF 2.1中的IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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