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

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

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

    飛翔的起點

    從這里出發(fā)

    導航

    <2008年12月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    統(tǒng)計

    常用鏈接

    留言簿(5)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    Spring和Struts整合的三種方式

    1,使用Spring 的 ActionSupport

    2,使用Spring 的 DelegatingRequestProcessor 類。

    3,全權委托。

    無論用那種方法來整合第一步就是要為struts來裝載spring的應用環(huán)境。 就是在 struts 中加入一個插件。

    struts-config.xml中

    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
                <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
                </plug-in>

    spring 的配置文件被作為參數(shù)配置進來。這樣可以省略對web.xml 文件中的配置。確保你的applicationContext.xml 在WEB-INF目錄下面

    1、使用Spring的ActionSupport .

    Spring 的ActionSupport 繼承至org.apache.struts.action.Action

    ActionSupport的子類可以或得 WebApplicationContext類型的全局變量。通過getWebApplicationContext()可以獲得這個變量。


    這是一個 servlet 的代碼:

    public class LoginAction extends org.springframework.web.struts.ActionSupport {
                public ActionForward execute(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response) {
                LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
                //獲得  WebApplicationContext  對象
                WebApplicationContext ctx = this.getWebApplicationContext();
                LoginDao dao = (LoginDao) ctx.getBean("loginDao");
                User u = new User();
                u.setName(loginForm.getName());
                u.setPwd(loginForm.getPwd());
                if(dao.checkLogin(u)){
                return mapping.findForward("success");
                }else{
                return  mapping.findForward("error");
                }
                }
                }
                applicationContext.xml 中的配置
                <beans>
                <bean id="loginDao" class="com.cao.dao.LoginDao"/>
                </beans>
                

    這中配置方式同直接在web.xml文件配置差別不大。

    注意:Action繼承自 org.springframework.web.struts.ActionSupport 使得struts和spring耦合在一起。

    但實現(xiàn)了表示層和業(yè)務邏輯層的解耦(LoginDao dao = (LoginDao) ctx.getBean("loginDao"))。


    2、使用Spring 的 DelegatingRequestProcessor 類

    DelegatingRequestProcessor  繼承自 org.apache.struts.action.RequestProcessor 并覆蓋了里面的方法。

    sturts-config.xml  中 

    processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/> 通過 來替代

    org.apache.struts.action.RequestProcessor 的請求處理。

    public class LoginAction extends Action {
                //利用spring來注入這個對象。
                private LoginDao dao ;
                public void setDao(LoginDao dao) {
                System.out.println("執(zhí)行注入");
                this.dao = dao;
                }
                public LoginDao getDao() {
                return dao;
                }
                public ActionForward execute(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response) {
                LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
                //這樣一改這行代碼似乎沒有必要了。
                //WebApplicationContext ctx = this.getWebApplicationContext();
                //LoginDao dao = (LoginDao) ctx.getBean("loginDao");
                User u = new User();
                u.setName(loginForm.getName());
                u.setPwd(loginForm.getPwd());
                //直接用dao來調(diào)用spring會將這個對象實例化。
                if(dao.checkLogin(u)){
                return mapping.findForward("success");
                }else{
                return  mapping.findForward("error");
                }
                }
                }
                這里的。
                LoginAction extends Action 說明 struts沒有和spring 耦合。
                看一下
                applicationContext.xml 中的配置。
                <beans>
                <bean id="loginDao" class="com.cao.dao.LoginDao"/>
                <bean name="/login" class="com.cao.struts.action.LoginAction">
                <property name="dao">
                <ref local="loginDao"/>
                </property>
                </bean>
                </beans>
                

    這里 name="/login" 與struts 中的path匹配

    class="com.cao.struts.action.LoginAction" 與struts 中的type匹配

    還要為 LoginAction 提供必要的setXXX方法。 獲得ApplicationCotext和依賴注入的工作都在DelegatingRequestProcessor中完成。

    3,全權委托:

    Action 的創(chuàng)建和對象的依賴注入全部由IOC容器來完成。使用Spring的DelegatingAcionProxy來幫助實現(xiàn)代理的工作

    org.springframework.web.struts.DelegatingActiongProxy繼承于org.apache.struts.action.Action .

    全權委托的配置方式同 方式 2 類似 (applcationContext.xml文件的配置和 Action類的實現(xiàn)方式相同)。

    <struts-config>
                <data-sources />
                <form-beans >
                <form-bean name="loginForm" 
      type="com.cao.struts.form.LoginForm" />
                </form-beans>
                <global-exceptions />
                <global-forwards />
                <action-mappings >
                <!-- type指向的是spring 的代理類 -->
                <action
                attribute="loginForm"
                input="login.jsp"
                name="loginForm"
                path="/login"
                scope="request"
                type="org.springframework.web.struts.DelegatingActionProxy" >
                <forward name="success" path="/ok.jsp" />
                <forward name="error" path="/error.jsp" />
                </action>
                </action-mappings>
                <message-resources parameter="com.cao.struts.ApplicationResources" />
                <plug-in className=
     "org.springframework.web.struts.ContextLoaderPlugIn">
                <set-property property="contextConfigLocation" 
      value="/WEB-INF/applicationContext.xml"/>
                </plug-in>
                </struts-config>
                不同之處
                1, <action>中 type指向的是spring 的代理類
                2, 去掉struts-config.xml中 <controller >
                


    三種整和方式中我們優(yōu)先選用 全權委托的方式。

    理由:

    1,第一種使得過多的耦合了Spring和Action .

    2,RequestProcessor類已經(jīng)被代理 如果要再實現(xiàn)自己的實現(xiàn)方式(如:編碼處理)怕有點麻煩。

    總結(jié)一下:

    整合工作中的步驟:

    1,修改struts-config.xml

    2, 配置applicationContext.xml

    3, 為Action添加get/set方法 來獲得依賴注入的功能。

    posted on 2008-12-08 17:00 forgood 閱讀(170) 評論(0)  編輯  收藏 所屬分類: struts+spring+hibernate


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


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 8888四色奇米在线观看免费看| 久久精品亚洲AV久久久无码| 中文字幕中韩乱码亚洲大片 | 亚洲精品无码久久久久YW| 亚洲天堂男人影院| 亚洲真人无码永久在线观看| jlzzjlzz亚洲jzjzjz| 亚洲人成7777| 亚洲av纯肉无码精品动漫| 久久亚洲AV成人无码国产最大| 亚洲AV无码一区二区一二区| 国产亚洲人成在线影院| 日韩在线一区二区三区免费视频| 国产黄在线播放免费观看| 中文字幕版免费电影网站| 日本免费久久久久久久网站| 99ee6热久久免费精品6| 国产成人福利免费视频| 我想看一级毛片免费的| 国产成人免费a在线视频色戒| 亚洲AV无码一区二三区| 亚洲日韩精品无码一区二区三区 | 在线播放亚洲第一字幕| 亚洲国产成人一区二区三区 | 免费看片免费播放| 日韩亚洲国产综合久久久| 久久精品国产亚洲Aⅴ蜜臀色欲| 亚洲精品美女久久久久99| 中文字幕亚洲色图| 亚洲视频在线观看2018| 羞羞视频免费网站入口| 免费网站观看WWW在线观看| h视频在线观看免费完整版| 拔擦拔擦8x华人免费久久| 久久久无码精品亚洲日韩软件| 亚洲AV美女一区二区三区| 亚洲永久网址在线观看| 成人A毛片免费观看网站| 国产91色综合久久免费| 国产免费黄色大片| 婷婷精品国产亚洲AV麻豆不片|