为什么屏幕上会显示异常而不是错误页面?

时间:2011-12-27 10:17:21

标签: java spring exception jsf

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

我的问题是为什么页面不会重定向到我的错误页面/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>

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


   An Error Occurred:
    Error creating bean with name 'melaketViewHandler' defined in 
ServletContext resource [/WEB-INF/JSFViewHandlersContext.xml]: Instantiation 
of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Could not instantiate bean class [com.ewave.meuhedet.view.melaketViewHandlers.MelaketViewHandler]: Constructor threw 
exception; nested exception is java.lang.NullPointerException

        - Stack Trace

        org.springframework.beans.factory.BeanCreationException: Error creating bean
     with name 'melaketViewHandler' defined in ServletContext resource 
    [/WEB-INF/JSFViewHandlersContext.xml]: Instantiation of bean failed; nested 
    exception is org.springframework.beans.BeanInstantiationException: Could not
     instantiate bean class [com.ewave.meuhedet.view.melaketViewHandlers.MelaketViewHandler]:
     Constructor threw exception; nested exception is java.lang.NullPointerException

      at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:254) 
...

2 个答案:

答案 0 :(得分:1)

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

[编辑] 为了更清楚:servlet中的某些代码(doGet()内部)捕获异常,然后执行相当于e.printStackTrace(out);的容器(即调用doGet())的代码永远不会看到异常,因此永远不会调用重定向到错误页面的代码。

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

答案 1 :(得分:1)

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

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();
  }
}