Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure spring HandlerExceptionResolver to handle NullPointerException thrown in jsp?

From a jsp is thrown a NullPointerException for example using <% null.toString(); %>

This exception is not handled by the HandlerExceptionResolver, but thrown to the web container(tomcat) and converted into a code 500 error.

How can I configure spring to get that error in my HandlerExceptionResolver ?

Details:

  • Spring can be configured to handle exceptions thrown inside Controllers, but not exceptions thrown by view.
  • Of course i can resolve the NullPointerException, but i want to design a solution that will gracefully resolve any possible problem on the web application in order to display a user friendly message to the user.
like image 348
raisercostin Avatar asked Oct 13 '08 01:10

raisercostin


People also ask

How do you handle exceptions in Spring application?

Spring MVC provides exception handling for your web application to make sure you are sending your own exception page instead of the server-generated exception to the user. The @ExceptionHandler annotation is used to detect certain runtime exceptions and send responses according to the exception.

How does Spring handle global exception?

Global Exception Handler - Exception Handling is a cross-cutting concern, it should be done for all the pointcuts in our application. We have already looked into Spring AOP and that's why Spring provides @ControllerAdvice annotation that we can use with any class to define our global exception handler.

How does Spring controller handle exceptions?

@ExceptionHandler. The exception handler method takes in an exception or a list of exceptions as an argument that we want to handle in the defined method. We annotate the method with @ExceptionHandler and @ResponseStatus to define the exception we want to handle and the status code we want to return.


1 Answers

See the HandlerInterceptor interface instead. You'll want the afterCompletion method. You can then intercept the response and then set the appropriate header information to redirect to a container-configured error web page. You're right that Spring doesn't have this functionality, this is going to have to be specified by the web.xml which determines which codes map to which pages.

like image 95
MetroidFan2002 Avatar answered Oct 12 '22 22:10

MetroidFan2002