Spring MVC Multiple Controller (original) (raw)

Last Updated : 23 Jul, 2025

We may construct numerous controllers at once in Spring MVC. Each controller class must be annotated with the @Controller annotation. A Spring MVC example with numerous controllers can be found here. The procedure is as follows:

  1. In the case of Maven, load the spring jar files or add dependencies.
  2. Make your controller class.
  3. Provide a controller entry in the web.xml file.
  4. In a separate XML file, define the bean.
  5. Make the rest of the view components.
  6. Start the server and make the project available.

Example Project

Project Structure:

Project Structure

Step 1. Add dependencies to pom.xml

XML `

4.0.0 com.geeksforgeeks SpringMVCMultipleController war 0.0.1-SNAPSHOT SpringMVCMultipleController Maven Webapp http://maven.apache.org junit junit 3.8.1 test

<!-- 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> 
SpringMVCMultipleController

`

Step 2. Create the request page

Let's create a simple JSP page containing two links

index.jsp

HTML `

Geeksforgeeks Spring MVC Tutorials || Geeksforgeeks Spring Boot Tutorials

`

Step 3. Develop a controller class

Let's make two controller classes, each of which returns a different view page.

GfgController1.java

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

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 4. Provide the entry of controller in the web.xml file

web.xml

XML `

SpringMVC spring org.springframework.web.servlet.DispatcherServlet 1 spring /

`

Step 5. Define the bean in the XML file

spring-servlet.xml

XML `

<!-- 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>

`

Step 6. Create the other view components

gfgpage1.jsp

HTML `

Welcome to Geeksforgeeks Spring MVC Tutorial

`

gfgpage2.jsp

XML `

Welcome to Geeksforgeeks Spring Boot Tutorial

`