【问题标题】:How to handle Code Error 500 in Richfaces如何处理 Richfaces 中的代码错误 500
【发布时间】:2011-03-22 18:11:18
【问题描述】:

我使用的是 Richfaces 3.2.2,并且需要在出现异常时向用户显示 500 错误页面。问题是当我使用 ajax 事件时,当出现异常时,我无法向用户显示 500 错误。我已经在 web.xml 上定义了错误页面。

请原谅我的英语。请问有什么建议吗?

【问题讨论】:

    标签: jsf richfaces myfaces


    【解决方案1】:

    查看RichFaces developer guide 第 5.10.1 章。

    5.10.1 Request Errors Handling

    要在客户端执行自己的代码以防Ajax请求出错,需要重新定义标准的“A4J.AJAX.onError”方法:

    A4J.AJAX.onError = function(req, status, message){
        window.alert("Custom onError handler "+message);
    }
    

    以这种方式定义的函数接受作为参数:

    • req - 调用错误的请求的参数字符串
    • status - 服务器返回的错误编号
    • message - 给定错误的默认消息

    因此,可以创建自己的处理程序,在超时、内部服务器错误等情况下调用。

    因此,要显示服务器生成的错误响应,您需要执行以下操作:

    A4J.AJAX.onError = function(req, status, message){
        document.open();
        document.write(req.responseText);
        document.close();
    }
    

    重定向到错误页面,请执行以下操作:

    A4J.AJAX.onError = function(req, status, message){
        window.location = 'error.jsf';
    }
    

    您只需将强制性错误详细信息作为请求参数传递,或者按照 Odelya 的建议让服务器端将其存储在会话中。

    相关问题:

    【讨论】:

      【解决方案2】:

      由于您使用的可能是 JSF1.2 而不是 JSF2,因此您可以使用 FaceletViewHandler 来处理异常。

      public class CustomViewHandler extends FaceletViewHandler {
          ...
          @Override
          protected void handleRenderException(FacesContext context, Exception ex) throws IOException, ELException,
              FacesException {
              try {
                  ..
      
                  getSessionMap().put("GLOBAL_ERROR", ex);
                  getHttpResponseObject().sendRedirect("/error.jsf");
              } catch (IOException e) {
                  log.fatal("Couldn't redirect to error page", e);
              }
          }
      }
      

      当然需要在bean中处理,只需要从session中提取异常即可:

      Throwable ex = (Exception) getSessionMap().remove("GLOBAL_ERROR");
      

      【讨论】:

      • 不客气。请在 1 数字下方用 V(绿色)标记我的答案
      猜你喜欢
      • 2015-03-05
      • 1970-01-01
      • 2020-12-18
      • 2023-04-09
      • 1970-01-01
      • 2014-12-28
      • 2019-03-24
      • 1970-01-01
      • 2011-06-06
      相关资源
      最近更新 更多