The Front Controller design pattern

The Front Controller design pattern is a J2EE pattern; it provides solutions for the following application design problems:

  • In a web application based on the Model 1 architecture, too many controllers are required to handle too many requests. It is difficult to maintain and reuse them.
  • Each request has its own point of entry in the web application; it should be a single point of entry for each request.
  • JSP and Servlet are the main components of the Model 1 MVC pattern, so, these components handle both action and view, violating the Single Responsibility principle.

The Front Controller provides the solution to the aforementioned design problems of the web application. In a web application, it works as the main component which routes all requests into framework control. This means that too many requests land on a single controller (Front Controller), and then, these requests are delegated to the specific controllers. Front Controller provides centralized control, and improves the reusability and manageability, because, typically, only the resource is registered with the web container. This controller not only handles too many requests, but also has following responsibilities:

  • It initializes the framework to cater to the requests
  • It loads the map of all URLs and the components responsible for handling the request
  • It prepares the map for the views

Let's see the following diagram for Front Controller:

As you can see in the preceding diagram, all application requests land at the Front Controller, and it delegates these requests to the configured application controllers.

The Spring Framework provides a module based on the MVC pattern, that is, Model 2 architecture implementation. The Spring MVC module provides out-of-the-box front controller pattern implementation by introducing the org.springframework.web.servlet.DispatcherServlet class. This is a simple servlet class, and the backbone of the Spring MVC framework. And this Servlet is integrated with the Spring IoC container to benefit the Spring's dependency pattern. Spring's web framework uses Spring for its own configuration, and all controllers are Spring beans; these controllers are testable artifacts.

Let's dive into the internals of Spring MVC in this Chapter, and have a closer look at org.springframework.web.servlet.DispatcherServlet in the Spring MVC framework, and how it handles all incoming requests to the web application.

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

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