Explain Spring MVC PropertiesMethodNameResolver with example

We have already seen how we can configure MultiActionControllerPropertiesMethodNameResolver, a flexible MultiActionController method name resolver, to define the mapping between the URL and method name explicitly. See following example :

Now let us try to understand PropertiesMethodNameResolver with an example, first we will define our MultiActionController as we did in earlier post.

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 let us configure PropertiesMethodNameResolver 

<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.PropertiesMethodNameResolver">
      <property name="mappings">
	<props>
	   <prop key="/employee/c.htm">create</prop>
	   <prop key="/employee/u.htm">update</prop>
	   <prop key="/employee/d.htm">delete</prop>
	   <prop key="/employee/r.htm">read</prop>
	</props>
       </property>
     </bean>
    </property>
  </bean>

</beans>

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

  • EmployeeController –> /employee/*
  • /employee/c.htm –> create()
  • /employee/d.htm –> delete()
  • /employee/u.htm –> update()
  • /employee/r.htm –> read()

 

spring 12

FOLLOW US ON LinkedIn



Explore Tutu'rself