我的Blog我做主^_^

          走向一條通往JAVA的不歸路...

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            64 隨筆 :: 68 文章 :: 77 評論 :: 0 Trackbacks

          文檔: MultiActionController

          Spring提供一個多動作控制器,使用它你可以將幾個動作合并在一個控制器里,這樣可以把功能組合在一起。多動作控制器存在在一個單獨的包中——org.springframework.web.mvc.multiaction——它能夠將請求映射到方法名,然后調用正確的方法。比如當你在一個控制器中有很多公共的功能,但是想多個入口到控制器使用不同的行為,使用多動作控制器就特別方便。

          MultiActionController 提供的功能

          功能 解釋
          delegate MultiActionController有兩種使用方式。第一種是繼承MultiActionController,并在子類中指定由MethodNameResolver解析的方法(這種情況下不需要這個配置參數(shù)),第二種是你定義了一個代理對象,由它調用Resolver解析的方法。如果你是這種情況,你必須使用這個配置參數(shù)定義代理對象
          methodNameResolver 由于某種原因,MultiActionController需要基于收到的請求解析它必須調用的方法。你可以使用這個配置參數(shù)定義一個解析器

          一個多動作控制器的方法需要符合下列格式:

          				// actionName can be replaced by any methodname
          ModelAndView actionName(HttpServletRequest, HttpServletResponse);
          		

          由于MultiActionController不能判斷方法重載(overloading),所以方法重載是不允許的。此外,你可以定義exception handlers,它能夠處理從你指定的方法中拋出的異常。包含異常處理的動作方法需要返回一個ModelAndView對象,就象其它動作方法一樣,并符合下面的格式:

          				// anyMeaningfulName can be replaced by any methodname
          ModelAndView anyMeaningfulName(HttpServletRequest, HttpServletResponse, ExceptionClass);
          		

          ExceptionClass可以是任何異常,只要它是java.lang.Exceptionjava.lang.RuntimeException的子類。

          MethodNameResolver 根據(jù)收到的請求解析方法名。有三種解析器可以供你選擇,當然你可以自己實現(xiàn)解析器。

          • ParameterMethodNameResolver - 解析請求參數(shù),并將它作為方法名(http://www.sf.net/index.view?testParam=testIt的請求就會調用testIt(HttpServletRequest,HttpServletResponse))。使用paramName配置參數(shù)可以調整所檢查的參數(shù)

          • InternalPathMethodNameResolver - 從路徑中獲取文件名作為方法名(http://www.sf.net/testing.view的請求會調用testing(HttpServletRequest, HttpServletResponse)方法)

          • PropertiesMethodNameResolver - 使用用戶定義的屬性對象將請求的URL映射到方法名。當屬性定義/index/welcome.html=doIt,并且收到/index/welcome.html的請求,就調用doIt(HttpServletRequest, HttpServletResponse)方法。這個方法名解析器需要使用PathMatcher 所以如果屬性包含/**/welcom?.html,該方法也會被調用!

          我們來看一組例子。首先是一個使用ParameterMethodNameResolver和代理屬性<繼承MultiActionController>的例子,它接受包含參數(shù)名的請求

          例一:
          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使用的方法對應策略ParameterMehtodNameResolver,用于解析請求中的特定參數(shù)的值,將該值作為方法名調用-->
          <bean id="paramResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
          ??<property name="paramName" value="method"></property>
          ?</bean>
          <!--配置MultiActionController,因為使用delegate,所以需要配置delegate和methodNameResolver兩個屬性,兩個屬性分別指明該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" />

          測試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>

          測試URL:http://localhost:8080/xktj/muti.sp?method=view



          posted on 2007-03-02 11:23 java_蟈蟈 閱讀(8910) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網站導航:
           
          主站蜘蛛池模板: 黄龙县| 广汉市| 永年县| 龙江县| 碌曲县| 封丘县| 屯留县| 株洲市| 惠州市| 名山县| 林芝县| 霸州市| 新竹县| 彩票| 通州区| 吉木乃县| 修文县| 南乐县| 子长县| 虞城县| 车致| 岑溪市| 侯马市| 涟源市| 深州市| 胶州市| 湘潭县| 读书| 房产| 东乌珠穆沁旗| 牙克石市| 屏东县| 广昌县| 龙里县| 博湖县| 陇南市| 高阳县| 凉山| 西畴县| 桑植县| 大埔县|