DispatcherServlet¶
DispatcherServlet is the core component within the Spring MVC framework, functioning as the front controller for managing web requests.^[600-developer__java__spring__DispatcherServlet.md]
The operation of the DispatcherServlet relies on a set of collaborative strategies and components to process requests, render views, and handle exceptions.^[600-developer__java__spring__DispatcherServlet.md]
Core Processing Components¶
The request handling workflow is facilitated by several specialized components:
- HandlerMapping: Determines which controller (handler) should process the incoming request. Standard implementations include
BeanNameUrlHandlerMappingandRequestMappingHandlerMapping.^[600-developer__java__spring__DispatcherServlet.md] - HandlerAdapter: Acts as a bridge between the
DispatcherServletand the handler, allowing the servlet to invoke the handler regardless of the specific interface the handler implements. Common implementations includeHttpRequestHandlerAdapter,SimpleControllerHandlerAdapter, andRequestMappingHandlerAdapter.^[600-developer__java__spring__DispatcherServlet.md] - ViewResolver: Resolves logical view names (strings) returned by controllers into actual View objects. A primary implementation is
InternalResourceViewResolver.^[600-developer__java__spring__DispatcherServlet.md] - View: Responsible for rendering the actual model data, utilizing components like
DefaultRequestToViewNameTranslator.^[600-developer__java__spring__DispatcherServlet.md]
Exception Handling¶
The servlet utilizes a chain of HandlerExceptionResolver components to manage exceptions thrown during the handler mapping or execution phases.^[600-developer__java__spring__DispatcherServlet.md]
These resolvers typically operate in a specific order or hierarchy:
1. ExceptionHandlerExceptionResolver: Handles exceptions via @ExceptionHandler methods.
2. ResponseStatusExceptionResolver: Handles exceptions annotated with specific HTTP status codes.
3. DefaultHandlerExceptionResolver: Converts standard Spring exceptions into appropriate HTTP status codes.^[600-developer__java__spring__DispatcherServlet.md]
Additional Strategies¶
Beyond core request processing, DispatcherServlet uses additional strategies to support internationalization and theming:
- LocaleResolver: Determines the locale for the current request, enabling internationalization support.^[600-developer__java__spring__DispatcherServlet.md]
- ThemeResolver: Determines the theme to apply to the web application.^[600-developer__java__spring__DispatcherServlet.md]
Sources¶
^[600-developer__java__spring__DispatcherServlet.md]