Spring MVC - Multiple Controller

Last Updated : 19 Jun, 2026

Spring MVC allows you to create multiple controller classes within a single application. Each controller handles a different set of HTTP requests, making the application modular, organized, and easier to maintain. Instead of placing all request mappings in one class, you can separate related functionality into multiple controllers.

  • Organizes request handling into multiple controller classes.
  • Improves code readability and maintainability.
  • Makes large Spring MVC applications easier to manage.

Steps to Create Multiple Controllers in Spring MVC

Follow the steps below to create a Spring MVC application with multiple controllers.

Step 1: Create a Maven Project

  • Open STS IDE/Eclipse
  • Click File - New - Maven Project.
  • Select Create a simple project (Select archetype ) and click Next.

Then Enter the following details:

  • Group Id: com.gfg
  • Artifact Id: SpringMVCMultipleController
  • Packaging: war

Click Finish.

Step 2: Add Required Dependencies

Add the following maven dependencies and plugin to your pom.xml file:

XML
<project xmlns="https://maven.apache.org/POM/4.0.0"
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 
                             https://maven.apache.org/xsd/maven-4.0.0.xsd">
  
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.geeksforgeeks</groupId>
  <artifactId>SpringMVCMultipleController</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMVCMultipleController Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.1.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>  
        <groupId>javax.servlet</groupId>  
        <artifactId>servlet-api</artifactId>  
        <version>3.0-alpha-1</version>  
      </dependency> 
    
  </dependencies>
  <build>
    <finalName>SpringMVCMultipleController</finalName>
  </build>
</project>

Before moving into the coding part let's have a look at the file structure in the below image.

Project Structure

Step 3: Create the Home Page (index.jsp)

Create an index.jsp file that provides links to two different controllers.

HTML
<html>
<body>
  <a href="hello1">Geeksforgeeks Spring MVC Tutorials</a>  || 
  <a href="hello2">Geeksforgeeks Spring Boot Tutorials</a>
</body>
</html>

Step 4: Create Multiple Controller Classes

Create two controller classes and annotate each class with @Controller. Each controller handles a different URL and returns its own view.

GfgController1.java

The first controller maps the /hello1 request.

Java
package com.geeksforgeeks;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class GfgController1 {
  
    @RequestMapping("/hello1")
    public String display()
    {
        return "gfgpage1";
    }    
}

GfgController2.java

the second controller maps the /hello2 request.

Java
package com.geeksforgeeks;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class GfgController2 {
  
    @RequestMapping("/hello2")
    public String display()
    {
        return "gfgpage2";
    }    
}

Step 5: Configure DispatcherServlet (web.xml)

The web.xml file registers the Spring DispatcherServlet, which receives all incoming HTTP requests and forwards them to the appropriate controller.

XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <display-name>SpringMVC</display-name>
<servlet>  
    <servlet-name>spring</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>    
</servlet>  
<servlet-mapping>  
    <servlet-name>spring</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  
</web-app>

Step 6: Configure Spring MVC (spring-servlet.xml)

The spring-servlet.xml file is the central configuration file for the Spring MVC application. It scans controller classes, enables annotation-based configuration, and configures the view resolver to locate JSP files inside the WEB-INF/jsp folder.

XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context/"
    xmlns:mvc="http://www.springframework.org/schema/mvc/"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans/
        http://www.springframework.org/schema/beans//spring-beans.xsd
        http://www.springframework.org/schema/context/
        http://www.springframework.org/schema/context//spring-context.xsd
        http://www.springframework.org/schema/mvc/
        http://www.springframework.org/schema/mvc//spring-mvc.xsd">

    <!-- Add support for component scanning -->
    <context:component-scan base-package="com.geeksforgeeks" />

    <!--Add support for conversion, formatting and validation -->
    <mvc:annotation-driven/>
  
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>        
     </bean>
  
</beans>

Step 7: Create View Pages

Create two JSP pages inside the WEB-INF/jsp directory.

gfgpage1.jsp

Displays the Spring MVC tutorial page.

HTML
<html>
<body>
    
<p>Welcome to Geeksforgeeks Spring MVC Tutorial</p>

</body>
</html>

gfgpage2.jsp

Displays the Spring Boot tutorial page.

XML
<html>
<body>
    
<p>Welcome to Geeksforgeeks Spring Boot Tutorial</p>

</body>
</html>

Step 8: Run the Application

  • Right-click the project.
  • Select Run As -> Run on Server.
  • Choose Apache Tomcat Server.
  • Click Finish.

Open the following URL in your browser:

http://localhost:8080/SpringMVCMultipleController/

Output:

Output

When the user clicks the "GeeksforGeeks Spring MVC Tutorials" link, the Spring MVC tutorial page is displayed.

Output

When the user clicks the "GeeksforGeeks Spring Boot Tutorials" link, the Spring Boot tutorial page is displayed.

Output

Advantages of Using Multiple Controllers:

  • Better separation of application modules.
  • Easier to maintain and extend large projects.
  • Improves code readability and organization.
  • Different developers can work on different controllers simultaneously.

Limitations of Multiple Controllers:

  • Managing a large number of controllers can make the project structure more complex.
  • Similar logic may get duplicated across different controllers if not properly organized.
  • URL mappings must be unique to avoid request mapping conflicts.
Comment