Почему исключение отображается на экране вместо страницы ошибки? - PullRequest
4 голосов
/ 27 декабря 2011

Я использую 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>

Это сообщение об ошибке на моей странице:


   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 ]

1 голос
/ 27 декабря 2011

Мы используем пользовательский обработчик представления, который перехватывает исключения и перенаправляет на страницу ошибки:

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();
  }
}
1 голос
/ 27 декабря 2011

Потому что исключение никогда не попадает в контейнер сервлета.Где-то в трассировке стека вы найдете catch, который его обрабатывает.

[EDIT] Чтобы сделать это более понятным: некоторый код в сервлете (внутри doGet()) ловитисключение и затем делает эквивалент e.printStackTrace(out); - контейнер (то есть код, который вызвал doGet()) никогда не видит исключение, поэтому код для перенаправления на страницу ошибки никогда не вызывается.

Если вывы используете Eclipse: скопируйте трассировку стека в вашу среду IDE (см. Stacktrace Console ).Теперь вы можете нажать на каждый кадр стека, чтобы увидеть источник.Ищите все, что перехватывает исключение и превращает его в HTML.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...