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

5
我正在使用Java 6,JSF 1.2,Spring在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>

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

发生错误:
在ServletContext资源[/WEB-INF/JSFViewHandlersContext.xml]中定义的bean“ melaketViewHandler”创建失败:实例化bean失败;嵌套异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类[com.ewave.meuhedet.view.melaketViewHandlers.MelaketViewHandler]:构造函数抛出
异常;嵌套异常是java.lang.NullPointerException
- 堆栈跟踪
org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/JSFViewHandlersContext.xml]中定义的bean“ melaketViewHandler”创建失败:实例化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) ...
注意:本文中的html标记已被保留,不要删除。
2个回答

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中的<error-page>标签似乎被忽略了,因为facelets捕获了异常。 - dov.amir

1

因为异常从未到达servlet容器。在堆栈跟踪中,您会发现一个catch处理它。

[编辑] 为了更清晰地说明:servlet中的一些代码(在doGet()内部)捕获异常,然后执行相当于e.printStackTrace(out);的操作 - 容器(即调用doGet()的代码)从未看到异常,因此重定向到错误页面的代码从未被调用。

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


1
@DonRoby 这就是处理方式,servlet 将显示堆栈跟踪。 :) - Thomas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接