Spring MVC - Multiple Controller
Last Updated :
02 Mar, 2022
Improve
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:
- In the case of Maven, load the spring jar files or add dependencies.
- Make your controller class.
- Provide a controller entry in the web.xml file.
- In a separate XML file, define the bean.
- Make the rest of the view components.
- Start the server and make the project available.
Example Project
Project Structure:

Step 1. Add dependencies to pom.xml
<project 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
http://maven.apache.org/maven-v4_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>
Step 2. Create the request page
Let's create a simple JSP page containing two links
index.jsp
<html>
<body>
<a href="hello1">Geeksforgeeks Spring MVC Tutorials</a> ||
<a href="hello2">Geeksforgeeks Spring Boot Tutorials</a>
</body>
</html>
Step 3. Develop a controller class
Let's make two controller classes, each of which returns a different view page.
GfgController1.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
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 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 5. Define the bean in the XML file
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://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 6. Create the other view components
gfgpage1.jsp
<html>
<body>
<p>Welcome to Geeksforgeeks Spring MVC Tutorial</p>
</body>
</html>
gfgpage2.jsp
<html>
<body>
<p>Welcome to Geeksforgeeks Spring Boot Tutorial</p>
</body>
</html>
Output:


