Spring MVC ParameterMethodNameResolver tutorial with example


ParameterMethodNameResolver

        Implementation of MethodNameResolver which supports several strategies for mapping parameter values to the names of methods to invoke.

        The simplest strategy looks for a specific named parameter, whose value is considered the name of the method to invoke. 

        The name of the parameter may be specified as a JavaBean property, if the default action is not acceptable.

Example
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
           http://www.springframework.org/schema/util 
           http://www.springframework.org/schema/util/spring-util-3.0.xsd  
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <!-- ************************************************************* --> 
    
 <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
  
<!-- ************************************************************* --> 

<bean class="com.candidjava.springmvc.CustomerController">
    
    <property name="methodNameResolver">
        
        <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
            
	    	<property name="paramName" value="action"/>
	    	
		</bean>
		
   	</property>
    
 </bean>

<!-- ************************************************************* --> 

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    
	<property name="prefix">		    
		<value>/WEB-INF/pages/</value>
	</property>	
		
		
	<property name="suffix">
	 	<value>.jsp</value>
	</property>    
	    
</bean>

	<!-- ************************************************************* -->	
	
</beans>

package com.candidjava.springmvc;

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
	{
		ModelAndView model = new ModelAndView("page");
		model.addObject("msg", "add() method");
		return model;		

	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		ModelAndView model = new ModelAndView("page");
		model.addObject("msg", "delete() method");
		return model;		

	}
}


Screenshot
            

            

Download
        ParameterMethodNameResolver maven zip
        ParameterMethodNameResolver war


Comments


©candidjava.com