Spring Model View Controller

  • MVC (Model View Controller) is the separation of model, view and controller. It’s simply a paradigm; an ideal that you should have in the back of your mind when designing classes.
  • Avoid mixing code from the three categories into one class.

Model View Controller

Model: The model represents data and where business logic is performed, which may cause changes to data, like retrieving data from database, etc…

View: The view renders the contents of a model. It specifies exactly how the model data should be presented. If the model data changes, the view must update its presentation as needed.

Controller: The controller translates the user’s interactions with the view into actions that the model will perform.

What is View ?

The view is the interface the user sees and interacts with. For Web applications, this has historically been an HTML(with CSS and JavaScript) interface.

Handling all of these interfaces in your application is becoming increasingly challenging.

Advantage of MVC Design Pattern

  • A big advantage of MVC is that it handles the use of many different views for your application.
  • Common Model and Controllers can be used, for different views(like Desktop, Web based, Android App, iOS App, etc…)
  • There’s no real processing happening in the view; it serves only as a way to output data and allow the user to act on that data, whether it is an online store or an employee list.

What is Model?

The next component of MVC, the model, represents enterprise data and business rules. It’s where most of the processing takes place when using MVC. Databases fall under the model.

The data returned by the model is display-neutral, meaning that the model applies no formatting.

This way, a single model can provide data for any number of display interfaces. This reduces code duplication, because model code is only written once and is then reused by all of the views.

What is Controller?

Finally, the controller interprets requests from the user and calls portions of the model and next view as necessary to fulfill the request. So when the user clicks a Web link or submits an HTML form, the controller itself doesn’t output anything or perform any real processing. It takes the request and determines which model components to invoke and which formatting to apply to the resulting data.

To summarize the interaction steps

  1. A user request is interpreted by the controller

2. Controller determines what portions of the model and view to call.

3. The model handles interaction with data and applies business rules and then returns data.

4. Finally, the appropriate view is determined(by controller) and formatting is applied to the resulting data for presentation.

5. And then View gets displayed to the end user.