Open In App

Spring – Multi Action Controller with Example

Last Updated : 29 Mar, 2022
Like Article

Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier than compared to classic Java frameworks and application programming interfaces (APIs), such as Java database connectivity (JDBC), JavaServer Pages(JSP), and Java Servlet.

Multi-Action Controller: According to the Spring official documentation, MultiActionController is a Controller implementation that allows multiple request types to be handled by the same class. Subclasses of this class can handle several different types of requests with methods of the form.

So now let’s understand this line by a Spring MVC project example. 

Implementation: Project requirements are as follows: 

  1. Eclipse (EE version)/STS IDE
  2. Spring JAR Files
  3. Tomcat Apache latest version

Setting Up the Project

Note: We are going to use Spring Tool Suite 4 IDE for this project. Please refer to this article to install STS in your local machine How to Download and Install Spring Tool Suite (Spring Tools 4 for Eclipse) IDE? 

Step 1: Create a Dynamic Web Project in your STS IDE. You may refer to this article to create a Dynamic Web Project in STS: How to Create a Dynamic Web Project in Spring Tool Suite?

Step 2: Download the spring JARs file and go to the src > main > webapp > WEB-INF > lib folder and past these JAR files. 

Step 3: Refer to this article Configuration of Apache Tomcat Server and configure the tomcat server with your application. Now we are ready to go.

Configuring Dispatcher Servlet

Please refer to this article What is Dispatcher Servlet in Spring? and read more about Dispatcher Servlet which is a very very important concept to understand. Now we are going to configure Dispatcher Servlet with our Spring MVC application. 

Step 4: Go to the src > main > webapp > WEB-INF > web.xml file and the complete code for web.xml file is given below:

Step 5: Now go to the  src > main > webapp > WEB-INF and create an XML file. Actually, this is a Spring Configuration file like beans.xml file. And the name of the file must be in this format


For example, for this project, the name of the file must be


So either you can create a Spring Configuration File or you can just create a simple XML file add the below lines of code inside that file. 

Example: frontcontroller-dispatcher-servlet.xml


Creating Your Multi-Action Controller 


Step 6: Now, let’s create some controllers. Go to the src/main/java and create a new controllers package (For ex. com.student.controllers) as per your choice. And inside that create a Java class and name the class as DemoController. Now how to tell the Spring that this is our controller class. So the way we are going to tell the Spring is by marking it with a @Controller annotation.


public class DemoController {}

Note: Spring will automatically initialize the class having a @Controller annotation and register that class with the spring container.

Now let’s create some multiple handler methods inside this single controller class. So we can write something like this.  



// Java Program to Create Multiple Handler Methods
// Inside Single Controller Class
package com.student.controllers;
// Importing required classes
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
// Annotation
// Class
public class DemoController {
    public String helloWorld()
        return "Hello World!";
    public String welcomeGfgMessage()
        return "Welcome to GeeksforGeeks";
    public String happyCodingMesage()
        return "Happy Coding Guyz!";


So in the above code, we have created 3 handle methods (3 multiple request types)



Inside one controller class and spring allows us to do so. So we can say MultiActionController is a Controller implementation that allows multiple request types to be handled by the same class. 


Running Multi-Action Controller


Step 7: Add the below line inside the frontcontroller-dispatcher-servlet.xml file before running your application. 

<context:component-scan base-package="com.student.controllers"></context:component-scan>

Example: frontcontroller-dispatcher-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>
    <context:component-scan base-package="com.student.controllers"></context:component-scan>


Step 8: To run your Spring MVC Application right-click on your project > Run As > Run on Server and run your application as shown in the below image as follows: 



After that use the following URL to run your controller as shown in the below image. All other details are mentioned in the image. To run the ‘/hello‘ request hit the following URL



Similarly, to run the ‘/geeksforgeeks‘ request hit the following URL:



Output in the Web Page: 

Welcome to GeeksforGeeks

Lastly in order to run the ‘/happycoding‘ request hit the following URL as follows: 


Output in the Web Page:

Happy Coding Guyz!

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads