Spring MVC PropertiesMethodNameResolver example

PropertiesMethodNameResolver, a flexible MultiActionController method name resolver, to define the mapping between the URL and method name explicitly. See following example :

1. MultiActionController

A MultiActionController example.


package com.mkyong.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 CustomerController extends MultiActionController{
	
	public ModelAndView add(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		return new ModelAndView("CustomerPage", "msg","add() method");
			
	}
	
	public ModelAndView delete(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
			
		return new ModelAndView("CustomerPage", "msg","delete() method");
				
	}
	
	public ModelAndView update(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
			
		return new ModelAndView("CustomerPage", "msg","update() method");
				
	}
	
	public ModelAndView list(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
				
		return new ModelAndView("CustomerPage", "msg","list() method");
					
	}
	
}

2. PropertiesMethodNameResolver

With PropertiesMethodNameResolver, you can map whatever URL name to corresponds method name easily :


<beans ...>

 <bean 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
    
 <bean class="com.mkyong.common.controller.CustomerController">
   <property name="methodNameResolver">
    <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
      <property name="mappings">
	<props>
	   <prop key="/customer/a.htm">add</prop>
	   <prop key="/customer/b.htm">update</prop>
	   <prop key="/customer/c.htm">delete</prop>
	   <prop key="/customer/d.htm">list</prop>
	   <prop key="/customer/whatever.htm">add</prop>
	</props>
       </property>
     </bean>
    </property>
  </bean>

</beans>

Now, the URL will map to the method name in the following pattern :

  1. /customer/a.htm –> add() method
  2. /customer/b.htm –> update() method
  3. /customer/c.htm –> delete() method
  4. /customer/d.htm –> list() method
  5. /customer/whatever.htm –> add() method
Note
By default, MultiActionController is used the InternalPathMethodNameResolver to map URL to the corresponds method name.

Download Source Code

Reference

  1. PropertiesMethodNameResolver Javadoc
  2. Spring MVC MultiActionController example

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
samSpring MVC TutorialLaxminarayan RajputSpring MVC MultiActionController annotation example Recent comment authors
newest oldest most voted
sam
Guest
sam

I am try to remove response object from public ModelAndView add(HttpServletRequest request,
HttpServletResponse response)
if I removed response object my application doesn’t work. how can I make response object optional , since I am not using response object, sonarQube keeps complaining

thanks

trackback
Spring MVC Tutorial

[…] PropertiesMethodNameResolver example A flexible method name resolver for the MultiActionController controller class, it allow to define the mapping between requested URL and method name explicitly. […]

Laxminarayan Rajput
Guest
Laxminarayan Rajput

Hello MKYong, First of all, I like your website very much. I am new to Spring MVC and Security. I have two JSF based application which I need to make Spring security enabled. We want to login using Role based model. There are two role we have for these two applications. Example WebApp1 and WebApp2. their roles are App1 and App2. If user is login using App1 then he should be able to login to WebApp1 and If user is login using App2 role then he should be able to login WebApp2. Can you please help me to provide such… Read more »

trackback
Spring MVC MultiActionController annotation example

[…] you have to configure the method name resolver (InternalPathMethodNameResolver, PropertiesMethodNameResolver or ParameterMethodNameResolver) to map the URL to a particular method name. But, life is more […]