Spring MVC PropertiesMethodNameResolver tutorial with example


PropertiesMethodNameResolver

The most flexible out-of-the-box implementation of the MethodNameResolver interface. 
Uses java.util.Properties to define the mapping between the URL of incoming requests and the corresponding method name. Such properties can be held in an XML document.

Properties format is /welcome.html=displayGenresPage Note that method overloading isn't allowed, so there's no need to specify arguments.

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"> 
     <property name="caseSensitive" value="true" />
    
     
 </bean>
  
<!-- ************************************************************* --> 

<bean class=" com.candidjava.springmvc.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>	   				
				</props>
       		</property>
     	</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
        PropertiesMethodNameResolver maven zip
        PropertiesMethodNameResolver war


Comments


©candidjava.com