文檔:
MultiActionController
Spring提供一個(gè)多動(dòng)作控制器,使用它你可以將幾個(gè)動(dòng)作合并在一個(gè)控制器里,這樣可以把功能組合在一起。多動(dòng)作控制器存在在一個(gè)單獨(dú)的包中——org.springframework.web.mvc.multiaction——它能夠?qū)⒄?qǐng)求映射到方法名,然后調(diào)用正確的方法。比如當(dāng)你在一個(gè)控制器中有很多公共的功能,但是想多個(gè)入口到控制器使用不同的行為,使用多動(dòng)作控制器就特別方便。
MultiActionController
提供的功能
功能
|
解釋
|
delegate
|
MultiActionController有兩種使用方式。第一種是繼承MultiActionController,并在子類中指定由MethodNameResolver解析的方法(這種情況下不需要這個(gè)配置參數(shù)),第二種是你定義了一個(gè)代理對(duì)象,由它調(diào)用Resolver解析的方法。如果你是這種情況,你必須使用這個(gè)配置參數(shù)定義代理對(duì)象
|
methodNameResolver
|
由于某種原因,MultiActionController需要基于收到的請(qǐng)求解析它必須調(diào)用的方法。你可以使用這個(gè)配置參數(shù)定義一個(gè)解析器
|
一個(gè)多動(dòng)作控制器的方法需要符合下列格式:
// actionName can be replaced by any methodname
ModelAndView actionName(HttpServletRequest, HttpServletResponse);
由于MultiActionController不能判斷方法重載(overloading),所以方法重載是不允許的。此外,你可以定義exception handlers,它能夠處理從你指定的方法中拋出的異常。包含異常處理的動(dòng)作方法需要返回一個(gè)ModelAndView對(duì)象,就象其它動(dòng)作方法一樣,并符合下面的格式:
// anyMeaningfulName can be replaced by any methodname
ModelAndView anyMeaningfulName(HttpServletRequest, HttpServletResponse, ExceptionClass);
ExceptionClass可以是任何異常,只要它是java.lang.Exception或java.lang.RuntimeException的子類。
MethodNameResolver
根據(jù)收到的請(qǐng)求解析方法名。有三種解析器可以供你選擇,當(dāng)然你可以自己實(shí)現(xiàn)解析器。
-
ParameterMethodNameResolver
- 解析請(qǐng)求參數(shù),并將它作為方法名(http://www.sf.net/index.view?testParam=testIt的請(qǐng)求就會(huì)調(diào)用testIt(HttpServletRequest,HttpServletResponse))。使用paramName配置參數(shù)可以調(diào)整所檢查的參數(shù)
-
InternalPathMethodNameResolver
- 從路徑中獲取文件名作為方法名(http://www.sf.net/testing.view的請(qǐng)求會(huì)調(diào)用testing(HttpServletRequest, HttpServletResponse)方法)
-
PropertiesMethodNameResolver
- 使用用戶定義的屬性對(duì)象將請(qǐng)求的URL映射到方法名。當(dāng)屬性定義/index/welcome.html=doIt,并且收到/index/welcome.html的請(qǐng)求,就調(diào)用doIt(HttpServletRequest, HttpServletResponse)方法。這個(gè)方法名解析器需要使用PathMatcher
所以如果屬性包含/**/welcom?.html,該方法也會(huì)被調(diào)用!
我們來(lái)看一組例子。首先是一個(gè)使用ParameterMethodNameResolver和代理屬性<繼承MultiActionController>的例子,它接受包含參數(shù)名的請(qǐng)求
例一:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
public class ProductController{
?
? public ModelAndView view(HttpServletRequest request, HttpServletResponse response) throws Exception {
????? response.getOutputStream().print("Viewing");
????? return null;
? }
? public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
??????
??????response.getOutputStream().print("index");
??????return null;
? }
}
*-servlet.xml配置:
<!--配置MultiActionController使用的方法對(duì)應(yīng)策略ParameterMehtodNameResolver,用于解析請(qǐng)求中的特定參數(shù)的值,將該值作為方法名調(diào)用-->
<bean id="paramResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
??<property name="paramName" value="method"></property>
?</bean>
<!--配置MultiActionController,因?yàn)槭褂胐elegate,所以需要配置delegate和methodNameResolver兩個(gè)屬性,兩個(gè)屬性分別指明該MultiActionController的方法解析策略和delegate-->
?<bean name="/disp.sp" class="org.springframework.web.servlet.mvc.multiaction.MultiActionController">
?????<property name="methodNameResolver" ref="paramResolver"></property>
?????<property name="delegate" ref="productController"></property>
?</bean>
<!--配置MultiActionController所依賴的delegate-->
???<bean id="productController" class="com.wz.xktj.controller.ProductController" />
測(cè)試URL:http://localhost:8080/xktj/disp.sp?method=view
例二:
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 TestMultiactionController extends MultiActionController {
?public ModelAndView view(HttpServletRequest request,HttpServletResponse response) throws Exception{
??
??response.getOutputStream().println("this is test!!!");
??return null;
?}
?
?public ModelAndView add(HttpServletRequest request,HttpServletResponse response) throws Exception{
??
??response.getOutputStream().println("test add......");
??return null;
?}
}
*-servlet.xml配置:
<bean id="MethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
? ??<property name="paramName" value="method"></property>
? ??<property name="defaultMethodName" value="view"></property>
? ?</bean>
? ?<bean name="/muti.sp" class="com.wz.xktj.controller.TestMultiactionController">
? ??<property name="methodNameResolver" ref="MethodNameResolver"></property>
?</bean>
測(cè)試URL:http://localhost:8080/xktj/muti.sp?method=view