【问题标题】:Why is the exception displayd on the screen instead of the error page?为什么异常显示在屏幕上而不是错误页面上?
【发布时间】:2012-01-28 10:24:18
【问题描述】:

我正在使用 Java 6、jsf 1.2、tomcat 上的 spring,如果我在某个页面超时后执行操作,则会出现以下异常。

我的问题是为什么页面没有被重定向到我的错误页面 /error/error.jsf?

这是 web.xml(我没有过滤器):

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/error/error.jsf</location>
</error-page>
  <error-page>
    <exception-type>java.lang.IllegalStateException</exception-type>
    <location>/error/error.jsf</location>
</error-page>
 <error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/error/error.jsf</location>
</error-page>
<error-page>
    <exception-type>org.springframework.beans.factory.BeanCreationException</exception-type>
    <location>/error/error.jsf</location>
</error-page>

这是我页面上的错误消息:

发生错误: 创建名称为 'melaketViewHandler' 的 bean 时出错 ServletContext 资源 [/WEB-INF/JSFViewHandlersContext.xml]:实例化 bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException: 无法实例化 bean 类 [com.ewave.meuhedet.view.melaketViewHandlers.MelaketViewHandler]:构造函数抛出 例外;嵌套异常是 java.lang.NullPointerException - 堆栈跟踪 org.springframework.beans.factory.BeanCreationException:创建 bean 时出错 在 ServletContext 资源中定义了名称“melaketViewHandler” [/WEB-INF/JSFViewHandlersContext.xml]:bean 实例化失败;嵌套的 异常是 org.springframework.beans.BeanInstantiationException: 不能 实例化 bean 类 [com.ewave.meuhedet.view.melaketViewHandlers.MelaketViewHandler]: 构造函数抛出异常;嵌套异常是 java.lang.NullPointerException 在 org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:254) ...

【问题讨论】:

    标签: java spring exception jsf


    【解决方案1】:

    因为异常永远不会进入 servlet 容器。在堆栈跟踪的某处,您会找到处理它的catch

    [编辑] 为了更清楚地说明这一点:servlet 中的某些代码(doGet() 内部)捕获异常,然后执行与 e.printStackTrace(out); 等效的操作 - 容器(即调用doGet()) 永远不会看到异常,因此永远不会调用重定向到错误页面的代码。

    如果您使用的是 Eclipse:将堆栈跟踪复制到您的 IDE(请参阅Stacktrace Console)。现在您可以单击每个堆栈帧来查看源代码。寻找任何捕获异常并将其转换为 HTML 的内容。

    【讨论】:

    • @DonRoby 就是这样处理,servlet 将显示堆栈跟踪。 :)
    【解决方案2】:

    我们正在使用自定义视图处理程序来捕获异常并重定向到错误页面:

    public class ExceptionHandlingFaceletViewHandler extends FaceletViewHandler { 
      ...
    
      protected void handleRenderException( FacesContext context, Exception exception ) throws IOException, ELException, FacesException {  
        try {
          if( context.getViewRoot().getViewId().matches( ".*/error.jsf" ) ) {
            /*
             * This is to protect from infinite redirects if the error page itself is updated in the
             * future and has an error
             */
            LOG.fatal("Redirected back to ourselves, there must be a problem with the error.xhtml page", exception );
            return;
          }
    
          String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
          getHttpResponseObject().sendRedirect( contextPath + "/error" );
        }
        catch( IOException ioe ) {
          LOG.fatal( "Could not process redirect to handle application error", ioe );
        }
      }
    
      private HttpServletResponse getHttpResponseObject() {
        return (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
      }
    }
    

    【讨论】:

    • 谢谢,它有效。奇怪,似乎没有 ExceptionHandlingFaceletViewHandler,web.xml 标记被简单地忽略,因为 facelets 捕获了异常。
    猜你喜欢
    • 2022-06-13
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多