处理AJAXified组件的JSF 2.0异常的正确方法是什么? [英] What is the correct way to deal with JSF 2.0 exceptions for AJAXified components?

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

问题描述

我已经设置了web.xml,所以任何java.lang.Throwable(即任何未捕获的异常或错误)将转发到一个错误页面。然而,对于AJAXified组件,异常不会通过这种机制路由到错误页面。



我的测试用例是一个简单的 CommandButton 绑定到总是抛出一个 RuntimeException 的动作方法。最好的做法是让action方法捕获异常并添加一个错误严重性类型的 FacesMessage 。这是人们做的吗有没有办法配置JSF,以便如果AJAXified组件的备份bean方法抛出异常,可以显示错误页面?

解决方案

p>您需要实现自定义 ExceptionHandler ,这样做在以下情况下基本上在ajax请求中发生异常时:

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

如果您想要 web.xml

code>错误页面考虑。您需要解析整个 web.xml 以找到错误页面位置。此外,当渲染响应中发生异常时,您将基本需要自己重建整个视图。 OmniFaces 组件库具有完全这样的异常处理程序, FullAjaxExceptionHandler 。您可以找到完整的源代码这里和展示示例 here



另请参见:




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.

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?

解决方案

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();

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.

See also:

这篇关于处理AJAXified组件的JSF 2.0异常的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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