Spring MVC Tutorial 01 – An introduction to MVC framework ( MVC Architecture )
In this video tutorial, we will learn what is a MVC design or architecture. We will first look at the core concept of MVC and then understand it further using a real life analogy.
For our readers who are unable to access the Video Tutorial!!!
In this tutorial will talk about spring MVC framework in detail right from the foundation to the explanation of the meaning of MVC and all its advance level concepts.
MVC stands for Model View Controller architecture -a guideline which says how you should write a code for developing an application having a UI ( User Interface)
A typical web application developed using a Servlet technology is explained as follows:
A client requests for a web page in the application and then based on the URL – the request is mapped to a particular servlet – the servlet processes the request and sends back “html along with data” as the response to the client’s browser.
Client requests for web page —–> the request is mapped to a particular servlet ( based on the url ) —–>the servlet processes the request —–>sends back the generated response ( html and data to the client’s browser )
In the same way, a typical web application developed using a JSP technology may look like this:-
Client requests for web page —–>request is mapped to a particular JSP page ( based on the URL ) —–>JSP processes the request —–> response sent to client ‘s browser
Many people in IT industry argued that the approach developers follow in both ways is not so good. A typical JSP page or a Servlet which developers write would have two things or responsibilities mixed together : that is ; the code which actually renders UI on the screen (i.e html code) and the code which executes some logic in order to provide data which is to be displayed on the UI. This mixing of two makes a UI developer and Business logic developer difficult to work independently as both resides in the same file and as a result it takes more time to develop such a web application. Further in case if something needs to be modified in an exisiting web application at a later stages – it is a real pain. For example if you are asked to change the UI layout for the entire web application.- things will be a little complex to modify if both are mixed together and further will require more development and testing efforts.
MVC says we can overcome all such problems to a greater extent if we write an application in such a way that UI code is never mixed with Business logic code ( as we do in a typical servlet or a JSP page ) and provided a generic guideline on how we can develop such a application.
It says – an application should always be divided into three components called M V C . Model , View and a Controller.
The Model represents data; the View is the UI part and the Controller manages the application flow (makes a call to some sort of service producing Model and then passes on the model to the View ) so, this how the overall flow works –
Requests comes to the application —–> controller analyses it —–> decision is made to call an appropriate Business service to get the Model—–> receives the model from the Business service —–> passes on this model to the View component to form the final output ( response which can be sent back to the client’s browser ).
In case you are developing a web application using Java; the model is typically a Java Object having some properties along with getters and setters method and, typically the view is html code displaying data by retrieving it from the model object. And, these Business services are also the Java Objects having some Business logic to produce a model object.This way a separate set of people who are good at UI design will concentrate independently on developing the UI part of a web page and the set of people who are good at writing Business logic will just concentrate on writing logic which can provide data or Model to the View.
There is a little confusion in definition of MVC; different people have different perception of MVC guidelines as there is no formal definition available for MVC. Different frameworks have implemented MVC concept in different ways. Its not a question whether someone is right and someone is wrong; only concern is Business logic and presentation should be developed as a separate component which is is the main aim of MVC guidelines.