English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring MVC multi-controller example

In Spring MVC, we can create multiple controllers at once. It is necessary to use @Controller Comment on the mappings of each controller class. Here, we see a Spring MVC example with multiple controllers. The steps are as follows:

Load the spring jar file or add dependencies in Maven Create a controller class Provide controller entries in the web.xml file Define beans in a separate XML file Create other view components Start the server and deploy the project


1Add dependencies to pom.xml

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

2Create a request page

Let's create a simple JSP page containing two links.

index.jsp

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

3Create a controller class

Let's create two controller classes, each returning a specific view page.

HelloController1.java

package com.w3codebox;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController1 {
@RequestMapping("/hello1)
}
    public String display()
    {
        return "viewpage1";
    }   
}

HelloController2.java

package com.w3codebox;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController2 {
@RequestMapping("/hello2)
}
    public String display()
    {
        return "viewpage2";
    }   
}

4Define controller entries in web.xml file

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <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>

5Define Bean in 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">
    <!-- Provide support for component scanning -->
    <context:component-scan base-package="com.w3codebox" />
    <!--Provide 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>/property>        
     </bean>
</beans>

6Create other view components

viewpage1.jsp

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

viewpage1.jsp

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

Output: