The Model-View-Controller (MVC) architecture can be used to create flexible and loosely coupled web applications. This architecture has three separate components, the model, the view, and the controller. In this topic, we will explore the functionality of each of these components and see how they can be implemented in Spring.
So let's talk about the components of the MVC architecture. Each of them handles a specific task, as follows:
In Spring, the MVC architecture is designed using a special component known as DispatcherServlet. The diagram below outlines the whole process completed by DispatcherServlet.

The DispatcherServlet will receive HTTP requests to be processed. It will then use a component called HandlerMapping to call the appropriate controller for the request. HandlerMapping is implemented in the web.xml file and provides the mapping from URL patterns to their required controllers. Once HandlerMapping is initialized, it will attempt to load the application context from the file named[servlet-name]-servlet.xml. This file will indicate the URLs that will be handled by DispatcherServlet. Once the HandlerMapping is completed, the Controller will take over and handle the request.
The Controller will take the request and process it as required. It will also set the model data and return the view name to be processed by DispatcherServlet. The Controller component is defined in our program using the @Controller annotation. To map values from the model to the view, the Controller will typically use the ModelMap object. With this object, you can use the addAttribute method in order to map variable names in the view to the values in the model.
Once the Controller is completed, DispatcherServletwill utilize the component called ViewResolver to get the required view, based on the name returned from the controller. Once the view is retrieved, the DispatcherServlet will pass the model data to the View, and return it to the requester to be rendered in the browser.
To implement MVC in Spring, we will use a special type of engine known as a template engine. These are JSP (the default one; we can write normal HTML pages, with variables that allow us to build dynamic pages with the data from our model.), Thymeleaf (allows us processing of HTML, XML, text, Javascript, and CSS filesn and it also allows for more complex templating) and Freemaker (can generate web pages, as well as source code, XML, configuration files, emails, and many other text-based formats).
Suppose you implement the cart page of an online shop. This page shouldn’t display the same data for everyone. It also doesn’t even show the same information every time for the same user. This page shows precisely the products a particular user has added to their cart. In figure 8.1, you find an example of a dynamic view presented with the Manning website’s cart functionality. Observe how requests to the same page manning.com/ cart receive different data in response. The information displayed is different, even if the page is the same. The page has dynamic content!

In this section, we implement a web app with a dynamic view. Most apps today need to display dynamic data to the user. Now, for a user’s request expressed through an HTTP request sent by the browser, the web app receives some data, processes it, and then sends back an HTTP response that the browser needs to display (figure 8.2). We’ll review the Spring MVC flow and then work on an example to demonstrate how the view can get dynamic values from the controller.

In the example we implemented at the end of chapter 6, the browser’s content was the same for every HTTP request for our page. Remember the Spring MVC flow (figure 8.3):