spring webwork 集成 三種方法

          1.External-Ref

          這種方法看起來比較煩瑣,(這里描述的和spring文檔里的有些區(qū)別,這種方法可按spring文檔里的做法)

          第一步:在web.xml里面增加一個listener,如下

          xml 代碼
          1. < listener > ? ??
          2. ???????? < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > ? ??
          3. </ listener > ? ??
          4. ??
          5. < listener > ? ??
          6. ???????? < listener-class > com.opensymphony.xwork.spring.SpringExternalReferenceResolverSetupListener </ listener-class > ? ??
          7. </ listener > ??

          第二步:在Spring里面配置類似Dao之類的bean,例如?

          xml 代碼
          1. < bean ? id = "myDAO" ? class = "com.ryandaigle.persistence.MyDAO" ? singleton = "true" ? /> ??

          第三步:配置XWork.xml,例如

          xml 代碼
          1. < package ? name = "default" ? extends = "webwork-default" ? ??
          2. ???????? externalReferenceResolver = "com.opensymphony.xwork.spring.SpringExternalReferenceResolver" >
          3. ???????? < interceptors > ? ??
          4. ???????????????? < interceptor ? name = "reference-resolver" ? class = "com.opensymphony.xwork.interceptor.ExternalReferencesInterceptor" /> ? ??
          5. ???????????????? < interceptor-stack ? name = "myDefaultWebStack" > ? ??
          6. ???????????????????????? < interceptor-ref ? name = "defaultStack" /> ? ??
          7. ???????????????????????? < interceptor-ref ? name = "reference-resolver" /> ? ??
          8. ???????????????? </ interceptor-stack > ? ??
          9. ???????? </ interceptors > ? ??
          10. ???????? ??
          11. ???????? < default-interceptor-ref ? name = "myDefaultWebStack" /> ? ??
          12. ???????? ??
          13. ???????? < action ? name = "myAction" ? class = "com.ryandaigle.web.actions.MyAction" > ? ??
          14. ???????????????? < external-ref ? name = "DAO" > myDAO </ external-ref > ? ??
          15. ???????????????? < result ? name = "success" ? type = "dispatcher" > ? ??
          16. ???????????????????????? < param ? name = "location" > /success.jsp </ param > ? ??
          17. ???????????????? </ result > ? ??
          18. ???????? </ action > ????????????????? ??
          19. </ package > ???


          2.SpringObjectFactory


          我一直用這種方法,因為以前覺得是xwork本身提供的方法,升級有保障.

          配置方法:
          第一步.在spring的 applicationContext.xml (根據(jù)實際情況決定) 里面定義你的action,例如?

          xml 代碼
          1. < bean ? name = "some-action" ? class = "fully.qualified.class.name" ? singleton = "false" > ? ??
          2. ???? < property ? name = "someProperty" > < ref ? bean = "someOtherBean" /> </ property > ? ??
          3. </ bean > ???

          可以看到,可以使用Spring的特性來給你的action設置屬性等,當然也可以使用Spring的攔截器等 (可以使用不一定等于推薦使用)

          注意一定是singleton="false",因為xwork的action是這樣要求的.

          第二步.在xwork.xml里定義你的action定義?

          xml 代碼
          1. < action ? name = "myAction" ? class = "some-action" > ? ??
          2. ???? < result ? name = "success" > view.jsp </ result > ? ??
          3. </ action > ???

          ?第三步.要使上面的關聯(lián)生效,還要用我們的SpringObjectFactory來替換Xwork的默認ObjectFactory.
          最新的SpringObjectFactory里面,有兩種方法,其中我覺得A方法更直觀一些.

          A:修改web.xml

          xml 代碼
          1. <!--?這個是spring的listener,可以改為你的自定義的spring的Listenter?--> ? ??
          2. < listener > ? ??
          3. ???????? < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > ? ??
          4. </ listener > ? ??
          5. ??
          6. ??
          7. <!--?這個必須在?Spring?ContextLoaderListener?的后面?--> ? ??
          8. < listener > ? ??
          9. ?? < listener-class > com.opensymphony.xwork.spring.SpringObjectFactoryListener </ listener-class > ? ??
          10. </ listener > ??

          ?B.在spring的applicationContext.xml (根據(jù)實際情況決定)里面定義一個bean,例如?

          xml 代碼
          1. < bean ? id = "spring-object-factory" ? class = "com.opensymphony.xwork.spring.SpringObjectFactory" ? ??
          2. ???????? init-method = "initObjectFactory" /> ??

          這樣Spring會自動調用initObjectFactory方法來替換Xwork的默認ObjectFactory

          3.ActionAutowiringInterceptor


          這個方法是最近出現(xiàn)的,可能是最簡潔的方法,但是不知道性能上有沒有問題,我覺得jdk1.4以后應該沒有任何問題吧,至于實際效果你的自己測試一下.

          第一步:配置web.xml?

          xml 代碼
          1. <!--?這個是spring的listener,可以改為你的自定義的spring的Listenter?--> ? ??
          2. < listener > ? ???
          3. ???? < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > ? ??
          4. </ listener > ? ??

          ???????
          ?第二步配置xwork.xml里面的攔截器

          xml 代碼
          1. < interceptors > ? ??
          2. ?? < interceptor ? name = "autowire" ? class = "com.opensymphony.xwork.spring.interceptor.ActionAutowiringInterceptor" > ? ??
          3. ???? < param ? name = "autowireStrategy" > @org.springframework.beans.factory.config.AutowireCapableBeanFactory@AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE </ param > ? ??
          4. ?? </ interceptor > ? ??
          5. ?? < interceptor-stack ? name = "autowireDefault" > ? ??
          6. ???? < interceptor-ref interceptor-ref = "autowire" ? /> ? ??
          7. ???? < interceptor-ref interceptor-ref = "defaultStack" ? /> ? ??
          8. ?? </ interceptor-stack > ? ??
          9. </ interceptors > ???

          ????????????????你的攔截器里都要包含autowire,例如這個autowireDefault就相當于以前的默認的攔截器了.
          其中的攔截器策略可以配置,上面配置的是根據(jù)類型,如果不配置,默認是根據(jù)名字.

          一共有四種策略:
          AUTOWIRE_CONSTRUCTOR
          AUTOWIRE_BY_TYPE
          AUTOWIRE_BY_NAME
          AUTOWIRE_AUTODETECT

          這種方法執(zhí)行原理就是查找你的action的所有字段,如果和Spring的定義bean有相同的,就自動設置.

          假設你的Spring的applicationContext.xml里面有這樣一個定義:

          xml 代碼
          1. < bean ? id = "userManager" ? class = "com.test.UserManager" ? /> ???

          ?如果你在xwork.xml 里面定義的某個action有一個字段叫userManager,那么在運行時刻就會自動被設置為Spring的配置文件里定義的Bean.

          posted on 2010-04-12 14:23 飛熊 閱讀(250) 評論(0)  編輯  收藏 所屬分類: struts2.0


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


          網(wǎng)站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          <2010年4月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          導航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 蒙阴县| 英超| 广州市| 内乡县| 沽源县| 宜兰县| 正镶白旗| 温宿县| 福安市| 新平| 全椒县| 永川市| 大庆市| 荣昌县| 金阳县| 平凉市| 镇康县| 江孜县| 鄯善县| 黑河市| 武川县| 西城区| 连南| 渭源县| 仁布县| 内黄县| 营口市| 鞍山市| 库尔勒市| 日喀则市| 沭阳县| 富民县| 瓮安县| 梅州市| 和顺县| 沂水县| 峨眉山市| 合阳县| 三河市| 安阳市| 贵定县|