Introduction To MVC

Hi welcome to in this tutorial, here we will discuss quick introduction about MVC pattern and about different components. MVC stands for Model View Controller as it is popularly called, is a software design pattern for developing web applications. A Model View Controller pattern is made up of the following three parts:
  • Model - The lowest level of the pattern which is responsible for maintaining data.
  • View - This is responsible for displaying all or a portion of the data to the user.
  • Controller - Software Code that controls the interactions between the Model and View.
MVC is popular as it isolates the application logic from the user interface layer and supports separation of concerns. Here the Controller receives all requests for the application and then works with the Model to prepare any data needed by the View. The View then uses the data prepared by the Controller to generate a final presentable response. 

Introduction To MVC
MVC in Real Life Story:

First Scenario----- 
A guy name "Saroj", he want to run a own Restaurant. He decide to manage this Restaurant alone without any other employee because he is a good cook.
Introduction To MVC
So he has Kitchen for preparing the food and table where customer is seated.
Introduction To MVC

Now one customer "Alex" come to Restaurant now there are following steps for serving to customer.
Step 1: Saroj get the order.
Introduction To MVC
Step 2: Saroj prepare the ordered food for Alex because there are no other employee in the Restaurant.
Introduction To MVC
Step 3: After food completely cooked Saroj decorate the food in well manner in the tray for serving to Alex.
Introduction To MVC
Step 4: Finally serve to Alex.
Introduction To MVC

So above four steps to serve customer by Saroj, but if Restaurant being famous then there are many customers will be coming. Now it is difficult to manage by Saroj alone. So he worried about to managing the Restaurant, Now he call his friend Dinesh, he is good java programmer. So Dinesh feel the problem of Saroj, his problem is like "Coding Everything in a Single Servlet
Introduction To MVC
Here 1. servlet get request parameters from user, 2. do processing on its like insert in database, read database populate response, 3. Format response as HTML and 4. after all return this HTML formatted response to the User.

Second Scenario----- 
Now Dinesh give the some idea to Saroj for hiring another employee for helping. Now he hire one employee Komal as a cook. Komal work on kitchen, he has only responsibility to cook a best food for customers. 
Step 1: Saroj get order from Alex.
Introduction To MVC

Step 2: Saroj pass the required info to the Cook Komal.

Introduction To MVC

Step 3: Saroj Decorate the food.

Introduction To MVC

Step 4: Saroj serve food to the Alex.

Introduction To MVC
Now it is slightly improvement in the service of Restaurant. Now his friend Dinesh feel it again as a programmer meet the following structure of request flow and Say "It is like using Business Service Bean!".
Introduction To MVC
1. Here Servlet get request parameters and 2. Passing the request parameters to business service bean instead of working on it itself. Business bean is responsible for interacting with database and return the raw response data to the servlet, 3. Now servlet prepare formatted HTML as a response and 4. return to the User.

Third Scenario----- 
Now Dinesh give another idea to the Saroj for improving service of Restaurant for better result. Idea is Saroj recruit another more employee as decorator "Adesh" for decorate the food, he is good decorator, having more experience in decoration. 

Step 1: Saroj get the Order from Alex. 
Introduction To MVC
Step 2: Saroj passes order to Komal, he is good in cooking.
Introduction To MVC
 Step 3: Saroj passes cooked food to the food decorator Adesh.
Introduction To MVC
Step 4: Serve tray to the Alex.
Introduction To MVC

Here every employee in the Restaurant has own role of service like Saroj is good dealer, Komal is a good cook and Adesh is good food decorator. So every person follow an own role without any headache.

Now his friend feel this service "That sounds like is MVC" . 
 because here Saroj is in the role of controller of MVC, Komal is in the role of Model of MVC and Adesh is in the role of View of MVC.

Introduction To MVC

The model
The model is responsible for managing the data of the application. It responds to the request from the view and it also responds to instructions from the controller to update itself.

The view
A presentation of data in a particular format, triggered by a controller's decision to present the data. They are script based templating systems like JSP, ASP, PHP and very easy to integrate with AJAX technology.

The controller
The controller is responsible for responding to user input and perform interactions on the data model objects. The controller receives the input, it validates the input and then performs the business operation that modifies the state of the data model.

Struts2 is a MVC based framework. In the coming chapters, let us see how we can use the MVC methodology within Struts2.

<<Previous <<   || Index ||   >>Next >>


  1. Great article!
    Do have a look at my point of view:

    1. Thanks friend...
      Your article is also good...