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

标签 java spring exception jsf

我正在使用 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>

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


   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) 
...

最佳答案

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

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

关于java - 为什么屏幕上显示的是异常而不是错误页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8643500/

相关文章:

c++ - 如果在生成 std​​::thread 后引发异常,则未捕获异常

python - 使用APScheduler时如何处理异常?

java - 如何传递 ehcache.xml 中声明的 ehCache 装饰器中的依赖项

java - 如何将属性值注入(inject)使用注释配置的 Spring Bean?

java - 获取数组长度时出错抛出异常

java - web.servlet.PageNotFound ,请求方法 'GET' 不支持,Spring

java - restTemplate 删除与正文

java - 在文件中查找一行并将其删除

java - 最适合存储日期和时间的 SQL 和 Java 数据类型

java - 删除停用词不能正确标记