Spring comes with a beautiful framework which has became popular over several years. It is Spring MVC. So now, what’s MVC? And why MVC? Firstly, we will learn what is Spring MVC and then list down some of its advantages and then we will see a sample code snippet and I will explain you all the snippets step by step.

MVC stands for Model-View-Controller. It is an architecture where the application is separated between these three modules. Application is loosely coupled which is very good advantage when you consider the object oriented design in java. Let us see its individual modules.

  • Model – This is a business data , or you can say simple POJO classes which is shared between controllers and views.
  • View – This is a User Interface where the model data is represented to the user using different views such as JSP,JSF.
  • Controller – This acts as an interface between Model and View components. It processes all the business logic and incoming requests, manipulate data using the Model component, and interact with the Views to render the final output.

Now consider below diagram and I will explain the flow of spring MVC.


As from the above diagram you can see the front controller,model,controller,view template. So the application code is divided into 3 parts – Controller,Model and Views

Whenever a request arrives, the servlet container takes the request. Usually we have a web.xml configured in our application. In the web.xml we have mappings to the incoming url. That incoming url is mapped to a servlet mapping which references a servlet class. Now here comes a new term called a dispatcher servlet. Depending on the url mapping a particular dispatcher servlet is called , the dispatcher servlet uses handler mappings to map this incoming url to a particualr controller.

In Spring MVC, the Controller class is annotated with @RequestMapping(some url).Thus depending upon the incoming url, particular Controller class is called. This Controller class does the further processing for business logic , model data is exchanged and once th processing is done, the Controller sends back the model data and the view name to the dispatcher servlet.

Dispatcher servlet gets the model and view and uses view template to decide to which view the model data should be passed.Once the view is decided response to the client is rendered through that view. This is the complete flow of Spring MVC.

Above I have given you a basic understanding of spring mvc. For more deep level information you can visit  Spring MVC