Spring MVC ParameterMethodNameResolver example

ParameterMethodNameResolver, a MultiActionController method name resolver to map URL to method name via request parameter name, and the parameter name is customizable through the “paramName” property. 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. ParameterMethodNameResolver

With ParameterMethodNameResolver configured, and define the parameter name thought the “paramName” property:


<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.ParameterMethodNameResolver">
	   <property name="paramName" value="action"/>
	</bean>
     </property>
   </bean>

</beans>

Now, the URL will map to the method name via the “action” request parameter name :

  1. /customer/*.htm?action=add –> add() method
  2. /customer/whatever.htm?action=add –> add() method
  3. /customer/*.htm?action=update –> update() method
  4. /customer/*.htm?action=delete –> delete() method
  5. /customer/*.htm?action=list –> list() method

P.S the “*” means any text.

Note
By default, MultiActionController is used the InternalPathMethodNameResolver to map URL to the corresponds method name.

Download Source Code

Reference

  1. ParameterMethodNameResolver 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, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
ChaitanyaSpring MVC TutorialTomShrikanthSpring MVC MultiActionController annotation example Recent comment authors
newest oldest most voted
trackback
Spring MVC Tutorial

[…] ParameterMethodNameResolver example Another method name resolver for the MultiActionController controller class, it allow to map URL to method name via requested parameter name. […]

Chaitanya
Guest
Chaitanya

Hi Mkyong,

I have a Question, I have X login page pointing to one LDAP enabled AD, now i want to redirect that X login page to another LDAP enabled AD? How can we do this?

Shrikanth
Guest
Shrikanth

Hi Mkyong!

I am getting error when I am trying to build this application. I am using netbeans and throws an error..

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property ‘methodNameResolver’ of bean class [controller.CustomerController]: Bean property ‘methodNameResolver’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1052)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:921)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
… 21 more

Thanks!
Shrikanth

Tom
Guest
Tom

Hi,

is your CustomerController extending MultiActionController?

trackback
Spring MVC MultiActionController annotation example

[…] configure the method name resolver (InternalPathMethodNameResolver, PropertiesMethodNameResolver or ParameterMethodNameResolver) to map the URL to a particular method name. But, life is more easier with annotation support, now […]