Hi welcome to this MVC 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. 

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.

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.

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.


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.

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. 

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
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 >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago