- 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 in Real Life Story:
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.
So he has Kitchen for preparing the food and table where customer is seated.
Now one customer "Alex" come to Restaurant now there are following steps for serving to customer.
Step 1: Saroj get the order.
Step 2: Saroj prepare the ordered food for Alex because there are no other employee in the Restaurant.
Step 3: After food completely cooked Saroj decorate the food in well manner in the tray for serving to Alex.
Step 4: Finally serve to Alex.
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"
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.
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.
Step 2: Saroj pass the required info to the Cook Komal.
Step 3: Saroj Decorate the food.
Step 4: Saroj serve food to the Alex.
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!".
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.
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.
Step 2: Saroj passes order to Komal, he is good in cooking.
Step 3: Saroj passes cooked food to the food decorator Adesh.
Step 4: Serve tray to the Alex.
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.
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.
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 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.