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

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

问题描述

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

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.

我拥有的测试用例是一个简单的 CommandButton,它与一个总是抛出 RuntimeException 的操作方法相关联.似乎最佳实践是让 action 方法捕获异常并添加类型为错误严重性的 FacesMessage .这是人们所做的吗?有没有办法配置 JSF,以便在 AJAXified 组件的支持 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 组件库就有这样一个异常处理程序,FullAjaxExceptionHandler.您可以找到完整的源代码 此处和展示示例此处.

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.

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

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