gpt4 book ai didi

ajax - 处理 AJAXified 组件的 JSF 2.0 异常的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-03 23:42:02 24 4
gpt4 key购买 nike

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

我的测试用例是一个简单的 CommandButton绑定(bind)到总是抛出 RuntimeException 的操作方法.似乎最好的做法是让操作方法捕获异常并添加 FacesMessage类型错误严重性。这是人们做的吗?有没有办法配置 JSF,以便在 AJAXified 组件的支持 bean 方法抛出异常时可以显示错误页面?

最佳答案

您需要实现自定义 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为此找到错误页面的位置。此外,当渲染响应期间发生异常时,您基本上需要自己重建整个 View 。 OmniFaces组件库正好有这样一个异常处理程序, FullAjaxExceptionHandler .您可以找到完整的源代码 here和展示示例 here .
也可以看看:
  • using ExternalContext.dispatch in JSF error handler causes corrupt page rendering
  • Session timeout and ViewExpiredException handling on JSF/PrimeFaces ajax request
  • 关于ajax - 处理 AJAXified 组件的 JSF 2.0 异常的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10449862/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com