<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 11,  comments - 28,  trackbacks - 0

    JpetStore中的Action與普通Struts的Action處理方式不一樣。遍歷JpetStore的src文件夾,并無一個具體的Action,那么它是如何來完成普通Struts的Action工作了?
    查看JpetStore的Struts.xml可以發(fā)現(xiàn),它的Action只有一個,即“org.apache.stuts.beanaction.Beanaction”。通過Eclipse查看beanaction.jar的源代碼,可以發(fā)現(xiàn)Beanaction繼承與普通Action,即具備普通的action的功能。那么它無具體Action的奧妙在哪,繼續(xù)研究BeanAction的代碼,截取BeanAction的excute方法中核心部分代碼如下:?
    /*遍歷此方法的業(yè)務(wù)邏輯可知,*的優(yōu)先級最高,如果是*,則不調(diào)用任何方法直接Forward,類似于ForwardAction*/?
    private?static?final?String?NO_METHOD_CALL?=?"*";?
    …….?
    /*所有的FormBean都繼承于BaseBean*/?
    BaseBean?bean?
    =?(BaseBean)?form;?
    ??????ActionContext.initCurrentContext(request,?response);?
    ??????
    if?(bean?!=?null)?{?
    ????????
    //?Explicit?Method?Mapping?
    /*下面是檢查struts.xml配置中是否有parameter屬性*/?
    ????????Method?method?
    =?null;?
    ????????String?methodName?
    =?mapping.getParameter();?
    ????????
    if?(methodName?!=?null?&&?!NO_METHOD_CALL.equals(methodName))?{?
    ??????????
    try?{?
    /*通過反射,根據(jù)得到的方法名稱取得方法的句柄*/?
    ????????????method?
    =?bean.getClass().getMethod(methodName,?null);?
    ????????????
    synchronized?(bean)?{?
    /*下面是關(guān)鍵一句,調(diào)用basebean擁有的接口ActionInterceptor的實現(xiàn)DefaultActionInterceptor,來完成具體方法的調(diào)用*/?
    ??????????????forward?
    =?bean.getInterceptor().intercept(new?ActionInvoker(bean,?method));?
    ????????????}
    ?
    ?????????……..?
    /*無Parameter屬性,檢查path路徑的最后一個/后的名稱,即為調(diào)用的方法名*/?
    ????????
    //?Path?Based?Method?Mapping?
    ????????if?(method?==?null?&&?!NO_METHOD_CALL.equals(methodName))?{?
    ??????????methodName?
    =?mapping.getPath();?
    ??????????
    if?(methodName.length()?>?1)?{?
    ????????????
    int?slash?=?methodName.lastIndexOf("/")?+?1;?
    ????????????methodName?
    =?methodName.substring(slash);?
    ????????????
    if?(methodName.length()?>?0)?{?
    ??????????????
    try?{?
    ????????????????method?
    =?bean.getClass().getMethod(methodName,?null);?
    ????????????????
    synchronized?(bean)?{?
    ??????????????????forward?
    =?bean.getInterceptor().intercept(new?ActionInvoker(bean,?method));?
    ????????????????}
    ?
    ?????????????……..?
    /*根據(jù)調(diào)用方法返回的String,得到頁面的轉(zhuǎn)移路徑*/?
    return?mapping.findForward(forward);?
    ?
    通過研究上面這段代碼,我們可知,JpetStore中沒有具體Action實現(xiàn)的關(guān)鍵原因即在于下面這幾句
    /*通過反射,根據(jù)得到的方法名稱取得方法的句柄*/?
    ????????????method?
    =?bean.getClass().getMethod(methodName,?null);?
    ????????????
    synchronized?(bean)?{?
    /*下面是關(guān)鍵一句,調(diào)用basebean擁有的接口ActionInterceptor的實現(xiàn)DefaultActionInterceptor,來完成具體方法的調(diào)用*/?
    ??????????????forward?
    =?bean.getInterceptor().intercept(new?ActionInvoker(bean,?method));?
    ????????????}
    ?
    即將原來Action中的excute方法的實現(xiàn)轉(zhuǎn)移到FormBean中,這樣實現(xiàn)顯得更為簡捷,方便。研究ActionInvoke,它的核心代碼如下:
    public?String?invoke()?{?
    ????
    try?{?
    ??????
    return?(String)?method.invoke(bean,?null);?
    ????}
    ?catch?(Exception?e)?{?
    ??????
    throw?new?BeanActionException("Error?invoking?Action.??Cause:?"?+?e,?e);?
    ????}
    ?
    ??}
    ?
    至此可知,它調(diào)用的是formbean中的函數(shù)。且從這段代碼可知,formbean的這類特殊函數(shù),此處稱為action方法,要符合兩個特征:1)無參數(shù);2)返回值為string,此返回string即是Struts-config.xml的全局或局部的forward。
    以上是整個beanaction的實現(xiàn)機制。個人感覺此種實現(xiàn)方法對于開發(fā)者而言已經(jīng)類似于ASP.NET的.aspx與.cs開發(fā)模式了。下面是通過實例來說明一下BeanAction如何控制formbean的
    ?
    Struts-config.xml的配置里有3種映射方式,來告訴BeanAction把控制轉(zhuǎn)到哪個form bean對象的哪個方法來處理。
    (1)parameter=”*’直接跳轉(zhuǎn);(2)Parameter中含具體的方法名;(3)Path中最后一個/后的方法名
    以這個請求連接為例http://localhost/jpetstore4/shop/viewOrder.shtml
    1. URL Pattern

    ???
    <action?path="/shop/viewOrder"?type="com.ibatis.struts.BeanAction"
    ????name
    ="orderBean"?scope="session"
    ????validate
    ="false">
    ????
    <forward?name="success"?path="/order/ViewOrder.jsp"/>
    ??
    </action>

    ?
    此種方式表示,控制將被轉(zhuǎn)發(fā)到"orderBean"這個form bean對象 的"viewOrder"方法(行為)來處理。方法名取"path"參數(shù)的以"/"分隔的最后一部分。
    2. Method Parameter?
    <action?path="/shop/viewOrder"?type="com.ibatis.struts.BeanAction"
    ????name
    ="orderBean"?parameter="viewOrder"?scope="session"
    ????validate
    ="false">
    ????
    <forward?name="success"?path="/order/ViewOrder.jsp"/>
    ??
    </action>

    ?
    此種方式表示,控制將被轉(zhuǎn)發(fā)到"orderBean"這個form bean對象的"viewOrder"方法(行為)來處理。配置中的"parameter"參數(shù)表示form bean類上的方法。"parameter"參數(shù)優(yōu)先于"path"參數(shù)。
    3. No Method call
    ?<action?path="/shop/viewOrder"?type="com.ibatis.struts.BeanAction"
    ????name
    ="orderBean"?parameter="*"?scope="session"
    ????validate
    ="false">
    ????
    <forward?name="success"?path="/order/ViewOrder.jsp"/>
    ??
    </action>

    此種方式表示,form bean上沒有任何方法被調(diào)用。如果存在"name"屬性,則struts把表單參數(shù)等數(shù)據(jù)填充到form bean對象后,把控制轉(zhuǎn)發(fā)到"success"。否則,如果name為空,則直接轉(zhuǎn)發(fā)控制到"success"。
    這就相當于struts內(nèi)置的org.apache.struts.actions.ForwardAction的功能
    <action?path="/shop/viewOrder"?type="org.apache.struts.actions.ForwardAction"
    ????parameter
    ="/order/ViewOrder.jsp?"?scope="session"?validate="false">
    ?
    </action>
    ?

    ?
    posted on 2007-01-12 16:34 滌生 閱讀(3883) 評論(8)  編輯  收藏


    FeedBack:
    # re: iBatis的JpetStore示例中MVC機制實現(xiàn)的研究,BeanAction,BaseBean
    2007-01-12 18:46 | 小武藏
    scope="session"
    不知道為什么要使用session呢?request不行么?
      回復(fù)  更多評論
      
    # re: iBatis的JpetStore示例中MVC機制實現(xiàn)的研究,BeanAction,BaseBean
    2007-01-13 11:46 | 江南白衣
    好文,滌生的分析方便了一大堆覺得BaseAction非常棒,又懶得去看去理解源碼,沒有理解之前又不敢用它的懶人,比如我:)  回復(fù)  更多評論
      
    # re: iBatis的JpetStore示例中MVC機制實現(xiàn)的研究,BeanAction,BaseBean[未登錄]
    2007-01-14 22:31 | jooroo
    好文!不知為何beanaction包只是試驗性質(zhì)的用一用,是否有些什么defect我們不知道?  回復(fù)  更多評論
      
    # re: iBatis的JpetStore示例中MVC機制實現(xiàn)的研究,BeanAction,BaseBean
    2007-01-15 13:21 | 滌生
    beanaction,這個是否有defect,不清楚了。我只是在學(xué)習(xí)這個JpetStore,把學(xué)習(xí)時的疑惑給理清楚。具體的beanaction有什么defect,需要深入的研究、使用才知道。  回復(fù)  更多評論
      
    # re: iBatis的JpetStore示例中MVC機制實現(xiàn)的研究,BeanAction,BaseBean
    2007-01-15 13:22 | 滌生
    為什么使用Session不使用request,這個應(yīng)該是由于它的實際需求+統(tǒng)一風格決定的。其實很多地方是可以使用request的  回復(fù)  更多評論
      
    # re: iBatis的JpetStore示例中MVC機制實現(xiàn)的研究,BeanAction,BaseBean
    2007-01-16 12:54 | 小武藏
    謝謝滌生,我明白了。
      回復(fù)  更多評論
      
    # re: iBatis的JpetStore示例中MVC機制實現(xiàn)的研究,BeanAction,BaseBean
    2008-01-22 22:27 | 龐宏濤
    我覺得這種方式有性能的損失,struts不是線程安全的,通過這種方式的話必須要求synchronized,也就是在一個時刻只能有一個用戶執(zhí)行 forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
    其他的都要排隊等待,在大并發(fā)量的系統(tǒng)上肯定不行,這種也只能做做例子什么的。  回復(fù)  更多評論
      
    # re: iBatis的JpetStore示例中MVC機制實現(xiàn)的研究,BeanAction,BaseBean
    2008-01-28 18:03 | it942
    怎么現(xiàn)在JpetStore又用BaseAction了。  回復(fù)  更多評論
      

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


    網(wǎng)站導(dǎo)航:
     
    <2007年1月>
    31123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    常用鏈接

    留言簿(5)

    隨筆檔案

    UML

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲AV日韩综合一区尤物| 亚洲黑人嫩小videos| 特级毛片免费播放| 精品少妇人妻AV免费久久洗澡| 亚洲一区二区三区免费在线观看| 亚洲男人的天堂在线va拉文| 久久亚洲精品国产精品婷婷 | 国产免费拔擦拔擦8x| 亚洲另类无码一区二区三区| 四虎影视免费在线| 女bbbbxxxx另类亚洲| 婷婷亚洲天堂影院| 黄桃AV无码免费一区二区三区| 国产成人综合亚洲AV第一页 | 一个人免费播放在线视频看片| 亚洲国模精品一区| 日本免费中文视频| 91亚洲性爱在线视频| 四虎影视免费在线| 一级毛片在线免费视频| 久久精品亚洲视频| 免费精品国产日韩热久久| 亚洲欧洲无卡二区视頻| 亚洲一区二区三区免费| 一级毛片在线观看免费| 亚洲入口无毒网址你懂的| 凹凸精品视频分类国产品免费| 香蕉视频在线免费看| 亚洲毛片基地日韩毛片基地| 手机看片久久国产免费| a级毛片100部免费观看| 亚洲成a人片在线看| 亚洲午夜av影院| 91精品成人免费国产片| 美女隐私免费视频看| 久久亚洲精品无码| 永久免费无码网站在线观看| 亚洲精品视频免费观看| 亚洲三级在线播放| 亚洲熟女少妇一区二区| 成年女人午夜毛片免费视频|