<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">Hi welcome to this MVC tutorial&comma; here we will discuss quick introduction about <b>MVC <&sol;b>pattern and about different components&period; <b>MVC <&sol;b>stands for <b><u>M<&sol;u>odel <u>V<&sol;u>iew <u>C<&sol;u>ontroller<&sol;b> as it is popularly called&comma; is a software design pattern for developing web applications&period; A Model View Controller pattern is made up of the following three parts&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><b>Model <&sol;b>&&num;8211&semi; The lowest level of the pattern which is responsible for maintaining data&period;<&sol;li>&NewLine;<li><b>View <&sol;b>&&num;8211&semi; This is responsible for displaying all or a portion of the data to the user&period;<&sol;li>&NewLine;<li><b>Controller <&sol;b>&&num;8211&semi; Software Code that controls the interactions between the Model and View&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b>MVC <&sol;b>is popular as it isolates the application logic from the user interface layer and supports separation of concerns&period; Here the Controller receives all requests for the application and then works with the Model to prepare any data needed by the View&period; The View then uses the data prepared by the Controller to generate a final presentable response&period;&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;27368&period;mvc&lowbar;&period;gif" &sol;><&sol;div>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p><b>MVC in Real Life Story&colon;<&sol;b><&sol;p>&NewLine;<p><i><b>First Scenario<&sol;b><&sol;i>&&num;8212&semi;&&num;8211&semi;&nbsp&semi; <br &sol;>&NewLine; A guy name &&num;8220&semi;<b>Saroj<&sol;b>&&num;8220&semi;&comma; he want to run a own Restaurant&period; He decide to manage this Restaurant alone without any other employee because he is a good cook&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc1&period;png" &sol;><&sol;div>&NewLine;<p><b>So he has Kitchen for preparing the food and table where customer is seated&period;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc2&period;png" &sol;><&sol;div>&NewLine;<p>&NewLine;Now one customer &&num;8220&semi;<b>Alex<&sol;b>&&num;8221&semi; come to Restaurant now there are following steps for serving to customer&period;<br &sol;>&NewLine;<b>Step 1&colon; Saroj get the order&period;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc3&period;png" &sol;><&sol;div>&NewLine;<p><b>Step 2&colon; Saroj prepare the ordered food for Alex because there are no other employee in the Restaurant&period;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc4&period;png" &sol;><&sol;div>&NewLine;<p><b>Step 3&colon; After food completely cooked Saroj decorate the food in well manner in the tray for serving to Alex&period;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc5&period;png" &sol;><&sol;div>&NewLine;<p><b>Step 4&colon; Finally serve to Alex&period;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc6&period;png" &sol;><&sol;div>&NewLine;<p>&NewLine;So above four steps to serve customer by Saroj&comma; but if Restaurant being famous then there are many customers will be coming&period; Now it is difficult to manage by Saroj alone&period; So he worried about to managing the Restaurant&comma; Now he call his friend <b>Dinesh<&sol;b>&comma; he is good java programmer&period; So Dinesh feel the problem of Saroj&comma; his problem is like &&num;8220&semi;<i><b>Coding Everything in a Single Servlet<&sol;b><&sol;i>&&num;8221&semi;&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc7&period;png" &sol;><&sol;div>&NewLine;<p>Here <b>1&period; servlet <&sol;b>get request parameters from user&comma; <b>2&period; do processing on its like insert in database&comma; read database populate response<&sol;b>&comma;<b> 3&period; Format response as HTML<&sol;b> and <b>4&period; after all return this HTML formatted response to the User<&sol;b>&period;<&sol;p>&NewLine;<p>&NewLine;<i><b>Second Scenario<&sol;b><&sol;i>&&num;8212&semi;&&num;8211&semi;&nbsp&semi; <br &sol;>&NewLine;Now Dinesh give the some idea to Saroj for hiring another employee for helping&period; Now he hire one employee <b>Komal as a cook<&sol;b>&period; Komal work on kitchen&comma;<b> he has only responsibility to cook a best food for customers&period;&nbsp&semi;<&sol;b><br &sol;>&NewLine;<b>Step 1&colon; Saroj get order from Alex&period;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc8&period;png" &sol;><&sol;div>&NewLine;<p><b><br &sol;>&NewLine;Step 2&colon; Saroj pass the required info to the Cook Komal&period;<br &sol;>&NewLine;<&sol;b> <&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc8-1&period;png" &sol;><&sol;div>&NewLine;<p><b><br &sol;>&NewLine;Step 3&colon; Saroj Decorate the food&period;<br &sol;>&NewLine;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc8-2&period;png" &sol;><&sol;div>&NewLine;<p><b><br &sol;>&NewLine;Step 4&colon; Saroj serve food to the Alex&period;<br &sol;>&NewLine;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc8-3&period;png" &sol;><&sol;div>&NewLine;<p>Now it is slightly improvement in the service of Restaurant&period; Now his friend Dinesh feel it again as a programmer meet the following structure of request flow and Say &&num;8220&semi;<i><b>It is like using Business Service Bean&excl;<&sol;b><&sol;i>&&num;8220&semi;&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc7-1&period;png" &sol;><&sol;div>&NewLine;<p><b>1&period; Here Servlet get request parameters<&sol;b> and 2&period; <b>Passing the request parameters to business service bean instead of working on it itself&period; Business bean is responsible for interacting with database and return the raw response data to the servlet&comma;<&sol;b> 3&period;<b> Now servlet prepare formatted HTML as a response<&sol;b> and 4&period; <b>return to the User<&sol;b>&period;<&sol;p>&NewLine;<p><i><b>Third Scenario<&sol;b><&sol;i>&&num;8212&semi;&&num;8211&semi;&nbsp&semi; <br &sol;>&NewLine;Now <b>Dinesh <&sol;b>give another idea to the <b>Saroj <&sol;b>for improving service of Restaurant for better result&period; Idea is Saroj recruit another more employee as decorator &&num;8220&semi;<b>Adesh<&sol;b>&&num;8221&semi; for decorate the food&comma; he is good decorator&comma; having more experience in decoration&period;&nbsp&semi;<&sol;p>&NewLine;<p><b>Step 1&colon; Saroj get the Order from Alex&period;<&sol;b>&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc9&period;png" &sol;><&sol;div>&NewLine;<p><b>Step 2&colon; Saroj passes order to Komal&comma; he is good in cooking&period;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc9-1&period;png" &sol;><&sol;div>&NewLine;<p><b>&nbsp&semi;Step 3&colon; Saroj passes cooked food to the food decorator Adesh&period;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc9-2&period;png" &sol;><&sol;div>&NewLine;<p><b>Step 4&colon; Serve tray to the Alex&period;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc9-3&period;png" &sol;><&sol;div>&NewLine;<p>&NewLine;Here every employee in the Restaurant has own role of service like Saroj is good dealer&comma; Komal is a good cook and Adesh is good food decorator&period; So every person follow an own role without any headache&period; <&sol;p>&NewLine;<p> Now his friend feel this service &&num;8220&semi;<i><b>That sounds like is MVC<&sol;b><&sol;i>&&num;8221&semi; &period;&nbsp&semi;<br &sol;>&NewLine;&nbsp&semi;because here <b>Saroj is in the role of controller of MVC<&sol;b>&comma; <b>Komal is in the role of Model of MVC<&sol;b> and <b>Adesh is in the role of View of MVC<&sol;b>&period;<&sol;p>&NewLine;<p><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;mvc7-2&period;png" &sol;><&sol;div>&NewLine;<p>&NewLine;<b>The model<&sol;b><br &sol;>&NewLine;The model is responsible for managing the data of the application&period; It responds to the request from the view and it also responds to instructions from the controller to update itself&period;<&sol;p>&NewLine;<p> <b>The view<&sol;b><br &sol;>&NewLine;A presentation of data in a particular format&comma; triggered by a controller&&num;8217&semi;s decision to present the data&period; They are script based templating systems like<b> JSP&comma; ASP&comma; PHP and very easy to integrate with AJAX technology<&sol;b>&period;<&sol;p>&NewLine;<p> <b>The controller<&sol;b><br &sol;>&NewLine;The controller is responsible for responding to user input and perform interactions on the data model objects&period; The controller receives the input&comma; it validates the input and then performs the business operation that modifies the state of the data model&period;<&sol;p>&NewLine;<p> Struts2 is a MVC based framework&period; In the coming chapters&comma; let us see how we can use the MVC methodology within Struts2&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;model-1-and-model-2-mvc-architecture&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;struts-2-baby-step-to-learn&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-struts-2-framework&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;struts-2-baby-step-to-learn&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-struts-2-framework&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '404'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

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…

4 years ago

Decorator Pattern using Lambda

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

4 years ago

Delegating pattern using lambda

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

4 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;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

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

4 years ago

Scheduler @Scheduled Annotation Spring Boot

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

4 years ago