Open In App
Related Articles

Spring – MVC Framework

Like Article
Save Article
Report issue

Spring MVC Framework follows the Model-View-Controller architectural design pattern which works around the Front Controller i.e. the Dispatcher Servlet. The Dispatcher Servlet handles and dispatches all the incoming HTTP requests to the appropriate controller. It uses @Controller and @RequestMapping as default request handlers. The @Controller annotation defines that a particular class is a controller. @RequestMapping annotation maps web requests to Spring Controller methods. The terms model, view, and controller are as follows:

  • Model: The Model encapsulates the application data.
  • View: View renders the model data and generates HTML output that the client’s browser can interpret.
  • Controller: The Controller processes the user requests and passes them to the view for rendering.

Dispatcher Servlet

Dispatcher Servlet is the front controller that manages the entire HTTP request and response handling process. Now, the question is What is Front Controller? It is quite simple, as the name suggests, when any web requests are made, the requests will come first to the front Controller which is nothing but the Dispatcher Servlet. The Front Controller stands first that is why it’s name is like this. After the requests comes into this, the dispatcher servlet accepts the requests and decides which controller will be suitable to handle these requests. Then it dispatches the HTTP requests to specific controller.

Spring Model-View-Controller Flow Diagram


Spring MVC Framework works as follows:

  1. All the incoming requests are intercepted by the DispatcherServlet that works as the front controller.
  2. The DispatcherServlet then gets an entry of handler mapping from the XML file and forwards the request to the controller.
  3. The object of ModelAndView is returned by the controller.
  4. The DispatcherServlet checks the entry of the view resolver in the XML file and invokes the appropriate view component.

Advantages of Spring MVC Framework

  • The container is used for the development and deployment of applications and uses a lightweight servlet.
  • It enables rapid and parallel development.
  • Development of the application becomes fast.
  • Easy for multiple developers to work together.
  • Easier to Update the application.
  • It is Easier to Debug because we have multiple levels in the application.

Disadvantages of Spring MVC Framework

  • It has high complexity to develop the applications using this pattern.
  • It is not suitable for small applications which affect the application’s performance and design.

Create Your First Spring MVC Application

Consider the following example:

Step 0: Setup your project with maven use the required archtype to get the required folders directory and configure the server with your project.
Step 1: Load the spring jar files or add the dependencies if Maven is used. Add the following dependencies in pom.xml



  <name>SpringMVC Maven Webapp</name


Step 2: Defining a Controller

Let us Create the Controller Class


public class HelloGeek { 
    public String display() 
        return "hello"


Step 3: Provide the name of the controller in the web.xml file as follows:
DispatcherServlet is the front controller in Spring Web MVC. Incoming requests for the HTML file are forwarded to the DispatcherServlet.



<?xml version="1.0" encoding="UTF-8"?> 
                   " id="WebApp_ID" version="3.0"


Step 4: We have to define the bean in a separate XML file. We have specified the view components in this file.  It is located in the WEB-INF directory.



<?xml version="1.0" encoding="UTF-8"?> 
    <!-- This element defines the base-package where
         DispatcherServlet will search the controller class. --> 
    <context:component-scan base-package="com.geek" /> 
    <!--Provide support for conversion,
        formatting and also for validation --> 


Step 5: Use JSP to display the message



<p>Spring MVC Tutorial!!</p>


Step 6: Start the server and run the project. The output is displayed as follows:

Spring MVC Tutorial!!

Last Updated : 08 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads