Why is the exception displayed on the screen instead of the error page?

I am using Java 6, jsf 1.2, spring in tomcat, and if I perform an operation after a timeout from a specific page, I get an exception below.

My question is: why the page is not redirected to my /error/error.jsf error page?

This is web.xml (I have no filters):

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

This error message is on my 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) 
 ...
+4
source share
2 answers

We use a custom view handler that catches exceptions and redirects to the error page:

 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
source

Because the exception never gets into the servlet container. Somewhere in the stack trace you will find a catch that processes it.

[EDIT] To make this clearer: some servlet code (inside doGet() ) catches an exception, and then the equivalent of e.printStackTrace(out); - a container (that is, code called doGet() ) never sees an exception, so code to redirect to a page with an error is never called.

If you are using Eclipse: copy the stack trace to your IDE (see Stacktrace Console ). Now you can click on each frame of the stack to see the source. Look for anything that catches the exception and turns it into HTML.

+1
source

Source: https://habr.com/ru/post/1388108/


All Articles