

Table of Contents
1. Overview
This article contains Spring MVC Multiple Controller Example. While working with spring application it difficult to manage every code in the same controller. i.e. if we are developing Employee Management System at the time We have different department link Admin, Employee, HR and those have different functionality.
Most of the file we manage those based on URL also, When we separate those action using URL it will be more helpful in spring-security also, based on URL we can provide authorization to access those or not.
like:
Admin URL starts with /admin/{actionName}
Hr URL starts with /hr/{actionName}
Employee URL starts with /employee/{actionName}
Above require will be fulfill using managing multiple controller examples as bellow.
2. Example
Technology Used:
- String 4.3.0
- Maven 2.3.2
- Tomcat 7
- Java 8
- IntelljIDEA 14
2.1 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.springapp</groupId> <artifactId>spring-hello-word-multi-controller</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>spring-hello-word-multi-controller</name> <description>Spring MVC Multiple Controller Example</description> <properties> <spring.version>4.1.1.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> </dependencies> <build> <finalName>spring-hello-work</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
2.2 AdminController.java
It will full-fill request like
/admin/sayWelcome
package com.springdemo; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("admin") public class AdminController { @RequestMapping(value = "sayWelcome",method = RequestMethod.GET) public String list(ModelMap model) { model.addAttribute("message", "Welcome to Admin Page."); return "admin"; } }
2.3 EmployeeController.java
It will full-fill request like
empoyee/sayWelcome
package com.springdemo; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("employee") public class EmployeeController { @RequestMapping(value = "sayWelcome",method = RequestMethod.GET) public String list(ModelMap model) { model.addAttribute("message", "Welcome to Employee Page."); return "employee"; } }
2.4 pages/admin.jsp
<html> <body> <h1>${message}</h1> </body> </html>
2.5 pages/employee.jsp
<html> <body> <h1>${message}</h1> </body> </html>
2.6 mvc-dispatcher-servlet.xml
<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" 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"> <context:component-scan base-package="com.springdemo"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
2.7 web.xml
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Multiple Controller Example</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2.8 Output:

Spring MVC Multiple Controller Example

Spring MVC Multiple Controller Example