What I want:
I want to provide a model to the HTTP 404 error page. Instead of writing a static error Page, which is specified within web.xml, I want to use an exception controller, which handles HTTP 404 errors.
What I did:
Removed error page tag from web.xml:
<error-page>
<error-code>404</error-code>
<location>/httpError.jsp</location>
</error-page>
and implemented the following exception handler methods inside my AbstractController class:
@ExceptionHandler(NoSuchRequestHandlingMethodException.class)
public ModelAndView handleNoSuchRequestException(NoSuchRequestHandlingMethodException ex) {
ModelMap model = new ModelMap();
model.addAttribute("modelkey", "modelvalue");
return new ModelAndView("/http404Error", model);
}
@ExceptionHandler(NullPointerException.class)
public ModelAndView handleAllExceptions(NullPointerException e) {
ModelMap model = new ModelMap();
model.addAttribute("modelkey", "modelvalue");
return new ModelAndView("/exceptionError", model);
}
What is does:
It works find for exceptions, but not for HTTP error code status 404. It seems like HTTP 404 errors are handled by DispatcherServlet by default. Is it possible to change this behaviour?
And how can I catch 404 errors in my exception handler?