Spring MVC Tutorial

Core Spring MVC

Spring MVC - Annotation

Spring MVC - Form Handling

Spring MVC with JSTL

Spring MVC with REST API

Spring MVC with Database

Spring MVC - Multi Action Controller with Example

The MultiActionController in Spring MVC allows you to define multiple actions within a single controller. Instead of having a single handleRequest method (as in the older style of Spring controllers), the MultiActionController lets you have multiple methods where each method can handle a specific action.

Here's how to use MultiActionController:

1. Create the Controller:

This controller will extend MultiActionController and will have multiple methods to handle different actions.

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/multi")
public class MyMultiActionController extends MultiActionController {

    public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("ResultPage");
        mav.addObject("message", "Add method called");
        return mav;
    }

    public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("ResultPage");
        mav.addObject("message", "Delete method called");
        return mav;
    }
}

2. Create the View (JSP):

Let's assume you have a JSP file named ResultPage.jsp under the /WEB-INF/views/ directory.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>MultiAction Example</title>
</head>
<body>
    <h2>${message}</h2>
</body>
</html>

3. Spring Configuration:

Configure the MultiActionController in your Spring MVC configuration XML.

<!-- Enable Annotations -->
<context:annotation-config/>

<!-- Scan for annotated components -->
<context:component-scan base-package="com.example.demo.controller"/>

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

<!-- MultiAction Controller Configuration -->
<bean name="/multi.htm" class="com.example.demo.controller.MyMultiActionController">
    <property name="methodNameResolver">
        <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
            <property name="mappings">
                <props>
                    <prop key="/multi/add.htm">add</prop>
                    <prop key="/multi/delete.htm">delete</prop>
                </props>
            </property>
        </bean>
    </property>
</bean>

Here, the PropertiesMethodNameResolver is used to map URLs to method names.

4. Access the Actions:

  • Accessing the URL /multi/add.htm will call the add method.
  • Accessing the URL /multi/delete.htm will call the delete method.

While MultiActionController provided a unique way to structure controllers, it's worth noting that the more modern approach in Spring MVC is to use the @Controller annotation with specific @RequestMapping annotations for each action. This provides cleaner separation and more flexibility.

  1. Spring MVC Multi Action Controller example:

    The MultiActionController in Spring MVC allows you to define multiple methods to handle different actions in a single controller. Here's an example:

    public class MyMultiActionController extends MultiActionController {
    
        public ModelAndView action1(HttpServletRequest request, HttpServletResponse response) {
            // Handle action1
            return new ModelAndView("action1View");
        }
    
        public ModelAndView action2(HttpServletRequest request, HttpServletResponse response) {
            // Handle action2
            return new ModelAndView("action2View");
        }
    }
    
  2. Spring MVC Multi Action Controller configuration:

    In older versions of Spring, configuring a MultiActionController involves defining it in the Spring configuration XML file.

    <bean name="/myController.htm" class="com.example.MyMultiActionController" />
    
  3. Spring 3 MultiActionController example:

    In Spring 3, you can use MultiActionController as follows:

    public class MyMultiActionController extends MultiActionController {
    
        public ModelAndView action1(HttpServletRequest request, HttpServletResponse response) {
            // Handle action1
            return new ModelAndView("action1View");
        }
    
        public ModelAndView action2(HttpServletRequest request, HttpServletResponse response) {
            // Handle action2
            return new ModelAndView("action2View");
        }
    }
    

    And in the XML configuration:

    <bean name="/myController.htm" class="com.example.MyMultiActionController" />
    
  4. Migrating from MultiActionController to annotated controllers in Spring MVC:

    To migrate from MultiActionController to annotated controllers, create a new class annotated with @Controller and define methods using @RequestMapping for each action. Update the Spring configuration to scan the new controllers.

    @Controller
    public class MyController {
    
        @RequestMapping("/action1")
        public ModelAndView action1(HttpServletRequest request, HttpServletResponse response) {
            // Handle action1
            return new ModelAndView("action1View");
        }
    
        @RequestMapping("/action2")
        public ModelAndView action2(HttpServletRequest request, HttpServletResponse response) {
            // Handle action2
            return new ModelAndView("action2View");
        }
    }
    

    And in the XML configuration:

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