Create First Spring MVC Application

Last Updated : 11 Jul, 2026

Spring MVC is a widely used framework that helps build structured web applications using the Model-View-Controller pattern. It simplifies request handling and application flow using plain Java classes. In this guide, you will learn how to create and run your first Spring MVC controller in Eclipse/Spring Tool Suite.

  • Uses DispatcherServlet to manage and route incoming requests
  • Supports view technologies like JSP or Thymeleaf for UI rendering
  • Allows easy data sharing between controller and view using Model objects

Key Concepts in Spring MVC

  • Model: The Model represents the application's data. It can be a Java object or a collection of objects that stores and transfers information between the controller and the view.
  • View: The View is responsible for displaying data to the user. Spring MVC supports multiple view technologies such as JSP, Thymeleaf, FreeMarker, and Velocity.
  • Controller: The Controller contains the application's business logic. It receives HTTP requests, processes them, and returns the appropriate response.
  • DispatcherServlet: The front controller in the Spring MVC architecture. It intercepts all incoming requests and delegates them to appropriate controllers for processing.

Prerequisites 

Steps to Build First Spring MVC Controller in Eclipse/Spring Tool Suite

Follow the below steps to run your first SpringMVC Controller.

Step 1: Create a Dynamic Web Project in your STS IDE

  • Open Eclipse IDE.
  • Go to File -> New -> Dynamic Web Project.
  • Enter Project Name: IPBlockFilterProject.
  • Target Runtime: Apache Tomcat (choose version installed).
  • Dynamic Web Module version: 3.1 (or compatible).
  • Click Finish.

Step 2: Add maven Dependencies

Add following dependencies in your pom.xml file.

XML
 xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         https://maven.apache.org/xsd/maven-4.0.0.xsd">

    4.0.0

    com.student
    myfirst-mvc-project
    0.0.1-SNAPSHOT
    war

    
        17
        17
        UTF-8

        
        6.2.6
    

    

        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        
        
            jakarta.servlet
            jakarta.servlet-api
            6.1.0
            provided
        

        
        
            jakarta.servlet.jsp.jstl
            jakarta.servlet.jsp.jstl-api
            3.0.2
        

        
            org.glassfish.web
            jakarta.servlet.jsp.jstl
            3.0.1
        

    

    
        myfirst-mvc-project
    


 

Step 3: Configure Apache Tomcat Server

Configure Apache Tomcat Server with the application. Now we are ready to go.

Step 4: Configuring Dispatcher Servlet

The DispatcherServlet acts as the front controller and handles all incoming HTTP requests.

XML

 xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
             http://xmlns.jcp.org/xml/ns/javaee
             http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    myfirst-mvc-project

    
        frontcontroller-dispatcher
        
            org.springframework.web.servlet.DispatcherServlet
        
        1
    

    
        frontcontroller-dispatcher
        /
    


Step 5: Create Spring Configuration File

Now go to the  src -> main -> webapp -> WEB-INF and create an XML file. Actually, this is a Spring Configuration file like beans.xml file.

XML

 xmlns="http://www.springframework.org/schema/beans/"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context/"
       xsi:schemaLocation="http://www.springframework.org/schema/beans/
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context/
        https://www.springframework.org/schema/context/spring-context.xsd">


Step 6: Creating Spring MVC Controller

@Controller marks the class as a Spring MVC controller.

Java
package com.student.controllers;

// Importing required classes
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

// Annotation
@Controller

// Class
public class DemoController {

    // Annotation
    @ResponseBody
    @RequestMapping("/hello")

    // Method
    public String helloWorld()
    {

        return "Hello World!";
    }
}

Note: Spring will automatically initialize the class having a @Controller annotation and register that class with the spring container.

Step 7: Enable Component Scanning

Add the below line inside the frontcontroller-dispatcher-servlet.xml file

frontcontroller-dispatcher-servlet.xml:

XML

 xmlns="http://www.springframework.org/schema/beans/"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context/"
       xsi:schemaLocation="http://www.springframework.org/schema/beans/
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context/
        https://www.springframework.org/schema/context/spring-context.xsd">

     base-package="com.student.controllers">


Step 8: Run the Application

  • Right-click Project
  • Run As
  • Run on Server

After that use the following URL to run your controller as shown in the below image. All other details can be perceived through below image as follows: 

http://localhost:8080/myfirst-mvc-project/student.com/hello

Comment

Explore