What I want:
I want to provide a model for the HTTP 404 error page. Instead of writing the static error page specified in web.xml, I want to use an exception controller that handles HTTP 404 errors.
What I've done:
Removed tag page with tags from web.xml:
<error-page> <error-code>404</error-code> <location>/httpError.jsp</location> </error-page>
and the following exception handler methods are implemented 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:
It works to look for exceptions, but not for the status of the HTTP 404 error code. It seems that HTTP 404 errors are handled by the default DispatcherServlet. Can this behavior be changed?
And how can I catch 404 errors in my exception handler?
source share