Explain Spring MVC MultiActionController with example

In Spring MVC application, MultiActionController is used to group related actions or behaviour into a single controller, the method handler have to follow below signature:

public (ModelAndView | Map | String | void) actionName(
		HttpServletRequest, HttpServletResponse [,HttpSession] [,CommandObject]);

Now let us try to understand MultiActionController with an example

package com.tuturself.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class EmployeeController extends MultiActionController{
	
	public ModelAndView create(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		return new ModelAndView("EmployeePage", "msg","add() method");
			
	}
	
	public ModelAndView delete(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
			
		return new ModelAndView("EmployeePage", "msg","delete() method");
				
	}
	
	public ModelAndView update(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
			
		return new ModelAndView("EmployeePage", "msg","update() method");
				
	}
	
	public ModelAndView read(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
				
		return new ModelAndView("EmployeePage", "msg","list() method");
					
	}
	
}

Now we have to configure ControllerClassNameHandlerMapping

<beans ...>

 <bean 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
    
  <bean class="com.tuturself.common.controller.EmployeeController" />
	
</beans>

Once aboove configuration is done all your default URL mapping to the method name will look like as below

  • EmployeeController –> /employee/*
  • /employee/create.htm –> create()
  • /employee/delete.htm –> delete()
  • /employee/update.htm –> update()
  • /employee/read.htm –> read()

In order to add prefix or suffix to default URL mapping to the method name one can configure InternalPathMethodNameResolver as below:

<beans ...>
 <bean 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
    
  <bean class="com.tuturself.common.controller.EmployeeController">
     <property name="methodNameResolver">
	<bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
	   <property name="prefix" value="test" />
	   <property name="suffix" value="Employee" />
	</bean>
     </property>
   </bean>
</beans>

Now, the default URL mapping to the method name will look like as below:

  • EmployeeController –> /employee/*
  • /employee/create.htm –> testcreateEmployee()
  • /employee/delete.htm –> testdeleteEmployee()
  • /employee/update.htm –> testupdateEmployee()
  • /employee/read.htm –> testreadEmployee()

 

spring 12

FOLLOW US ON LinkedIn



Explore Tutu'rself