loading table of contents...

4.3.9.1. Explicit error handling in controllers

Spring's DispatcherServlet is responsible for finding and executing a handler and rendering the view. A handler is first located by matching the request properties. Then the request will be bound to a handler method, including locating and calling appropriate type converters. Then the handler itself will be called to construct a ModelAndView. As mentioned above, the handler is the place for the application to decide whether a request is valid or should be answered with an error response.

To keep controllers and views separate, it is good practice to return a model representing the error case instead of generating the error response in the controller itself. For this purpose the CAE provides the HttpError class and utility methods in HandlerHelper to create error models. A default view for HttpError will set an appropriate HTTP status code and can be overwritten to generate more sophisticated error pages. See Section 4.3.1.1, “Building the Model” for details.