Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Spring MVC - Multiple Controller
Next article icon

Spring MVC - Multiple Controller

Last Updated : 02 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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
<?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
<?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
<html> <body>      <p>Welcome to Geeksforgeeks Spring MVC Tutorial</p>  </body> </html> 

gfgpage2.jsp

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

Output:

Output Output Output

Next Article
Spring MVC - Multiple Controller

S

sanketnagare
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Java-Spring
  • Java-Spring-MVC
Practice Tags :
  • Java

Similar Reads

    Spring MVC - Create Registration Form using Form Tag Library
    Spring Framework provides spring’s form tag library for JSP views in Spring’s Web MVC framework. In Spring Framework, we use Java Server Pages(JSP) as a view component to interact with the user. From version 2.0, Spring Framework provides a comprehensive set of data binding-aware tags. These tags ar
    7 min read
    Data Binding in Spring MVC with Example
    Data Binding, as the name itself, is a self-explanatory word. In data binding what we have to do is we have to capture or store the data so that we can bind that data with another resource (for example displaying the data in the frontend part) as per our needs or we can also read the data from a var
    8 min read
    Spring MVC - Pagination with Example
    We will be explaining how we can implement pagination in Spring MVC Application. This is required when we need to show a lot of data on pages. Suppose in an e-commerce site we have a lot of products but we can't show all of those on a single page, so we will show only 20 products on each page. This
    3 min read
    Spring MVC Integration with MySQL
    Spring MVC is one of the most popular Java frameworks for building scalable web applications. When combined with MySQL, it provides a robust solution for developing data-driven applications. This article will guide you through integrating Spring MVC with MySQL, covering database setup, project confi
    7 min read
    OpenSource REST API URL and Retrieving Data From it By Using Spring MVC
    In this internet era, a lot of helper services are available in the form of REST API and mostly REST API provides us the details in the form of JSON/XML. We can use them in our applications and render the data as we like. Here is the list of a few opensource REST API URLs: API NameDescriptionURLCoin
    6 min read
    How to Resolve WEB xml is missing and failOnMissingWebXml is set to true in Eclipse/STS?
    Eclipse/STS IDE is generally used to develop Spring applications and what happens is whenever we are creating a simple Maven project and if the web.xml is missing or you have deleted that file then you may encounter this problem inside the pom.xml file corresponding to which do refer to the below im
    2 min read
    Spring MVC Application Without web.xml File
    Spring MVC framework enables separation of modules namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain java classes. Here we will be creating and running Your First Spring MVC Application,
    4 min read
    Spring - MVC Listbox
    Spring Web MVC framework to demonstrate how to utilize Listbox in forms. Let's start by setting up an Eclipse IDE and then following the steps to create a Dynamic Form-based Web Application utilizing the Spring Web Framework. The items are listed in the Spring MVC form Listbox. This tag creates a se
    4 min read
    Spring MVC - Multiple Resolver Mapping
    Spring MVC Framework provides the feature of View Resolver through which you can map a view to its respective view and render the model in your browser. The View Resolver eliminates the use of view technologies like Velocity and JSP. There are many view resolvers available in spring-like InternalRes
    4 min read
    How to Extract TV Show Details via REST API and Spring MVC?
    REpresentational State Transfer (REST) is an architectural style that defines a set of constraints to be used for creating web services. REST API is a way of accessing web services in a simple and flexible way without having any processing. Spring MVC is a Web MVC Framework for building web applicat
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences