Loading
Spring MVC Multiple Controller
Why Do We Use Multiple Controllers?

In a real-world Spring MVC project, an application may have different modules (like User, Admin, Product, Order, etc.).
To keep your application modular and organized, we define separate controller classes for each module.



Setup of Multiple Controllers in Spring MVC (Step by Step)


1. Add Maven Dependencies (pom.xml)

Make sure these basic dependencies are added:

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.25</version>
  </dependency>

  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
  </dependency>

  <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
  </dependency>
</dependencies>


2. Create Directory Structure 

src/ ├── main/ │ ├── java/ │ │ └── com.quipoin.controller/ │ │ ├── HomeController.java │ │ └── AdminController.java │ ├── resources/ │ ├── webapp/ │ ├── WEB-INF/ │ │ ├── views/ │ │ │ ├── home.jsp │ │ │ └── admin.jsp │ │ └── dispatcher-servlet.xml │ └── web.xml


3. Create Controller Classes

HomeController.java

package com.quipoin.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {
    @RequestMapping("/home")
    public ModelAndView homePage() {
        return new ModelAndView("home", "message", "Welcome to Home Page!");
    }
}


AdminController.java

package com.quipoin.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AdminController {
    @RequestMapping("/admin")
    public ModelAndView adminPage() {
        return new ModelAndView("admin", "message", "Welcome to Admin Dashboard!");
    }
}


4. Configure Dispatcher Servlet ( web.xml )

<web-app>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>


5. Create Spring Configuration ( dispatcher-servlet.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <context:component-scan base-package="com.quipoin.controller" />
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>


6. Create JSP Views

home.jsp

<html>

<body>
    <h2>${message}</h2>
</body>

</html>


admin.jsp

<html>

<body>
    <h2>${message}</h2>
</body>

</html>



Run the Project

  • Hit http://localhost:8080/yourProject/home - loads home.jsp
  • Hit http://localhost:8080/yourProject/admin - loads admin.jsp


You can now easily use multiple controller classes in one Spring MVC project.
It helps in organizing code by functionality and improves readability.
This is especially helpful in large applications where different teams may work on different modules.