处理AJAXified组件的JSF 2.0异常的正确方法是什么? [英] What is the correct way to deal with JSF 2.0 exceptions for AJAXified components?
问题描述
我的测试用例是一个简单的 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
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:
- using ExternalContext.dispatch in JSF error handler causes corrupt page rendering
- Session timeout and ViewExpiredException handling on JSF/PrimeFaces ajax request
这篇关于处理AJAXified组件的JSF 2.0异常的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!