什么是对付JSF 2.0例外Aj​​ax化组件的正确方法? [英] What is the correct way to deal with JSF 2.0 exceptions for AJAXified components?

查看:146
本文介绍了什么是对付JSF 2.0例外Aj​​ax化组件的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了web.xml文件,这样任何的java.lang.Throwable的(即任何未捕获的异常或错误)将转发到一个错误页面。不过,对于Ajax化组件,异常不得到通过此机制发送到错误页面。

I have set up web.xml so that anything that's java.lang.Throwable (i.e. any uncaught exceptions or errors) will forward to an error page. However, for AJAXified components, exceptions dont get routed to the error page via this mechanism.

测试情况下,我有一个简单的命令捆绑的操作方法总是抛出一个的RuntimeException 。这似乎是最好的做法是有作用的方法捕获异常,并添加一个类型错误严重的的FacesMessage 。这是什么人呢?有没有一种方法来配置JSF这样,如果一个支持Ajax组件的支持bean方法抛出一个异常,该错误页面可以显示?

The test case I have is a simple CommandButton tied to an action method that always throws a RuntimeException. It seems like the best practice would be to have the action method catch the exception and add a FacesMessage of type error severity. Is this what people do? Is there a way to configure JSF so that if an AJAXified component's backing bean method throws an exception that the error page can be shown?

推荐答案

您需要实现一个自定义的 的ExceptionHandler 这其中确实的基本的的异常时在Ajax请求发生以下

You need to implement a custom ExceptionHandler for this which does basically the following when an exception occurs in an ajax request:

String errorPageLocation = "/WEB-INF/errorpages/500.xhtml";
context.setViewRoot(context.getApplication().getViewHandler().createView(context, errorPageLocation));
context.getPartialViewContext().setRenderAll(true);
context.renderResponse();

如果你想利用的web.xml 的错误页面,考虑到这是不完全微不足道。你需要解析整个的web.xml 此找到错误页面的位置。此外,当发生异常时给予回应,那么你基本上需要自己重建整个视图。该 OmniFaces 组件库正好有这样一个异常处理函数,<一个href="http://omnifaces.org/docs/javadoc/current/org/omnifaces/exceptionhandler/FullAjaxExceptionHandler.html"相对=nofollow> FullAjaxExceptionHandler 。你可以找到完整的源$ C ​​$ C <一个href="https://github.com/omnifaces/omnifaces/blob/master/src/main/java/org/omnifaces/exceptionhandler/FullAjaxExceptionHandler.java"相对=nofollow>此处和展示的例子这里

This is not exactly trivial if you want to take web.xml error pages into account. You'd need to parse the entire web.xml for this to find the error page locations. Also, when the exception occurred during render response, then you'd basically need to rebuild the whole view yourself. The OmniFaces component library has exactly such an exception handler, the FullAjaxExceptionHandler. You can find the full source code here and the showcase example here.

  • <一个href="http://stackoverflow.com/questions/15474345/using-externalcontext-dispatch-in-jsf-error-handler-causes-corrupt-page-renderin/15476224#15476224">using ExternalContext.dispatch在JSF中的错误处理程序引起了腐败的页面渲染
  • <一个href="http://stackoverflow.com/questions/11203195/session-timeout-and-viewexpiredexception-handling-on-jsf-primefaces-ajax-request/11206114#11206114">Session超时和ViewExpiredException处理对JSF / PrimeFaces Ajax请求
  • using ExternalContext.dispatch in JSF error handler causes corrupt page rendering
  • Session timeout and ViewExpiredException handling on JSF/PrimeFaces ajax request

这篇关于什么是对付JSF 2.0例外Aj​​ax化组件的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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