1. 程式人生 > >Spring mvc 中MultiActionController的三種methodNameResolver

Spring mvc 中MultiActionController的三種methodNameResolver

三種methodNameResolver分別為:

org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver

org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver

org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver

配置方法為:

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"
	default-lazy-init="true">
	
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="urlMap">  
		<map>  
			<entry key="/abc/*"><ref bean="empFormController"></ref></entry>  
		</map>  
	</property> 
	
	</bean>
	<bean id="empFormController" class="test.EmpMultiController">
		<property name="methodNameResolver" ref="cResolver">
		</property>
	</bean>
	<!-- 定義三種解析策略 -->
	<bean id="aResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />
	<bean id="bResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
		 <property name="mappings">  
      		<value> /abc/test.do=doTest</value>  
		</property>  
	</bean>
	<bean id="cResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
		<property name="paramName" value="func" />
	</bean>
	
	<bean id="startController" class="test.StartController" />
	<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>
	

控制器程式碼:

package test;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class EmpMultiController extends MultiActionController{
	
	public ModelAndView to_add(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mav = new ModelAndView("add_emp");
		mav.addObject("deptList", new String[]{"sales","manage"});
		return mav;
	}
	
	public ModelAndView doTest(HttpServletRequest request, HttpServletResponse response){
		System.out.println("doTest");
		return null;
	}
	
	public ModelAndView add_emp(HttpServletRequest request, HttpServletResponse response, 
			 Emp emp){
		System.out.println(emp);
		return new ModelAndView("success");
	}

	@Override
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Date.class, new MyEditor());
	}
	
}


當methodNameResolver分別設定為

aResolver訪問方式為:

cResolver訪問方式為:

(aaa部分是我隨便加的,沒有也可以,只要保證?func=doTest即可)