Have you heard much about the MVC pattern? I too, but really today when I learn more about Spring MVC, I discovered that it has long been misunderstood about this model. So, today I would like to write this tutorial to introduce you to the MVC model in the most easily understood.
The original MVC model
The Model View Controller (MVC) was first introduced by Trygve Reenskaug when he was working on the Smalltalk project at Xerox PARC (a California company in the US) in the 1970s. At that time, this model is applicable to desktop applications.
The original MVC model divides the presentation layer into different components. Each component will assume a different role.
- The view will use the Model to display the results to the user.
- Based on the user’s actions, View will enable the Controller to handle the user’s actions.
- Then the Controller will update the data for the Model.
- The Model will prompt View to display the results to the user.
You can see the following figure to better understand:
This model works very well with desktop applications, but applying this model to web applications is not feasible due to the nature of the HTTP protocol in web applications.
With a web application, the user interacts with the application by sending a request to the server that runs the web application. The web application handles this request, updates the view, and then sends it back to the user. This means we have to have a different approach in the web environment, rather than push the changes to the View, then these changes will be brought back from the server.
That’s why we have to make changes in the MVC model for the web environment.
Model MVC in a web environment
To overcome the difficulty of the MVC model in the web environment, a Front Controller has been introduced. In other words, Front Controller will be the first place to receive requests from users and it will also return results to users. The Front Controller will route the request to the appropriate controllers, which control the request, return the model, and select which view to use.
Please see the figure below:
In Java, in most cases, Front Controller is an implementation of the javax.servlet.Servlet interface. In Spring MVC, this object is org.springframework.web.servlet.DispatcherServlet.