DispatcherServlet explained

DispatcherServlet is the gateway to any Spring MVC application. Inherited from javax.servlet.http.HttpServlet, it is typically configured declaratively in the web.xml file. While you can have multiple definitions of DispatcherServlet with unique URL patterns, most Spring MVC applications only have single DispatcherServlet with the context-root URL(/), that is, all requests coming to that domain will be handled by DispatcherServlet.

Starting from Servlet 3.0, in addition to declarative configuration in the web.xml file, DispatcherServlet can be configured programmatically by implementing or extending either of these three support classes provided by Spring:

  • The WebAppInitializer interface
  • The AbstractDispatcherServletInitializer abstract class
  • The AbstractAnnotationConfigDispatcherServletInitializer abstract class

The following code listing demonstrates how to implement a WebAppInitializer directly in your application:

public class ApplicationInitializer implements WebApplicationInitializer {

  private static final Logger logger = LoggerFactory.getLogger(ApplicationInitializer.class);

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {

    logger.info("===== Application is starting up! ========");
    XmlWebApplicationContext appContext = new XmlWebApplicationContext();
    appContext.setConfigLocation("/WEB- INF/spring/appServlet/servlet- context.xml");

    ServletRegistration.Dynamic registration = servletContext.addServlet("rootDispatcher", new DispatcherServlet(appContext));
    registration.setLoadOnStartup(1);
    registration.addMapping("/");
  }

WebApplicationContext – ApplicationContext for the Web

DispatcherServlet uses a specialized ApplicationContext called WebApplicationContext that has many web request processing capabilities. It is aware of which ServletContext it is associated with and is also capable of resolving themes. This interface has concrete implementations for specific contexts such as XML, @Configuration annotated classes, and portlets. By default, DispatcherServlet uses XMLWebApplicationContext. When DispatcherServlet is loaded, it looks for the bean configuration file of WebApplicationContext and initializes it.

WebApplicationContext objects are hierarchical. Every Spring MVC application has root ApplicationContext (configurable with a context-param tag called contextConfigLocation in the web.xml file), and each Servlet, including DispatcherServlet, has its own child context (configurable by its own init-param, contextConfigLocation). Ideally, Servlet-specific child contexts have beans customizing that Servlet, and root ApplicationContext has all shared beans.

Beans supporting DispatcherServlet and their roles

Upon receiving a web request, DispatcherServlet performs a set of operations in sequence as part of the request processing, with the help of a set of supporting beans. This table lists these special beans and their responsibilities:

Bean

Responsibilities

HandlerMapping

Maps incoming web requests to handlers and pre- and post-processors

HandlerAdapter

Invokes the handler which resolves arguments and dependencies, such as annotated arguments for URL-mapped controller method endpoints

HandlerExceptionResolver

Allows programmatic handling of exceptions and maps exceptions to views

ViewResolver

Resolves logical view names to view instances

LocaleResolver

Resolves the client's locale in order to enable internationalization

LocaleContextResolver

A richer extension of LocaleResolver, with timezone information

ThemeResolver

Resolves themes configured in your app for enhanced user experience

MultipartResolver

Handles multipart file uploads as part of HTTP requests

FlashMapManager

Manages FlashMap instances that store temporary Flash attributes between requests redirected from one another

DispatcherServlet is extremely flexible; we can even create and configure custom implementations for all these beans. However, Spring MVC provides a set of nice implementations by default so that you don't need to customize or provide your own implementations unless absolutely required. These default implementations can be found inside org.springframework.web.servlet.DispatcherServlet.properties. If you override them with your own implementation of any of these beans, yours will override the defaults.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset