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

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

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

    小毅總結(jié)之--->spring整合struts的三種方式

    Posted on 2008-10-12 00:45 H2O 閱讀(861) 評(píng)論(1)  編輯  收藏 所屬分類: Spring+Struts+Hibernate整合 spring整合struts的三種方式" trackback:ping="http://www.tkk7.com/xiaoyi/services/trackbacks/233816.aspx" /> -->
    方式一:使用 Spring 的 ActionSupport或DispachActionSupport 類整合 Structs
    Action

    package com.yz.actions.action;

    import java.lang.reflect.InvocationTargetException;
    import javax.servlet.http.*;
    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.struts.action.*;
    import org.springframework.web.struts.DispatchActionSupport;
    import com.yz.services.*;
    import com.yz.actions.form.*;
    import com.yz.vo.UserVo;
    //spring的action管理請(qǐng)求
    public class UserAction extends DispatchActionSupport {
        
    //針對(duì)接口編程
        IUserServices ius;
        
        
    public IUserServices getIus() {
            
    return ius;
        }


        
    public void setIus(IUserServices ius) {
            
    this.ius = ius;
        }


        
    public ActionForward test(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response) 
    {
            UserForm userForm 
    = (UserForm) form;
            UserVo uv 
    = new UserVo();
            
    try {
                BeanUtils.copyProperties(uv, userForm);
            }
     catch (Exception e) {
                    e.printStackTrace();
            }

            
    /*IUserServices ius = new IUserServicesImpl();*/
            
    //通過(guò)父類的方法獲取spring容器 并得到其中的bean對(duì)象
            ius =(IUserServices)super.getWebApplicationContext().getBean("ius");
            boolean add = ius.addUsers(uv);
            System.out.println(uv.getUserName());
            String url 
    = "/error.jsp";
            
    if(add){
                url 
    = "/ok.jsp";
            }

            System.out.println(url);
            
    return new ActionForward(url) ;
        }

    }

    自己寫(xiě)的請(qǐng)求處理器,處理中文亂碼
    package com.yz.myReq;

    import java.io.UnsupportedEncodingException;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.RequestProcessor;

    public class myReq extends RequestProcessor 

    struts-config.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

    <struts-config>
      
    <data-sources />
      
    <form-beans >
        
    <form-bean name="userForm" type="com.yz.actions.form.UserForm" />

      
    </form-beans>

      
    <global-exceptions />
      
    <global-forwards />
      
    <action-mappings >
        
    <action
          
    attribute="userForm"
          name
    ="userForm"
          path
    ="/user"
          scope
    ="request"
          type
    ="com.yz.actions.action.UserAction" 
          parameter
    ="actions"
       
    />
      
    </action-mappings>
      
    <controller processorClass="com.yz.myReq.myReq"></controller>
      
    <message-resources parameter="com.yz.actions.ApplicationResources" />
     
    <!-- 添加一個(gè)插件,用來(lái)在啟動(dòng)struts框架的時(shí)候,
          同時(shí)讀取spring配置文件,啟動(dòng)spring容器 
    -->
      
    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
              
    <set-property property="contextConfigLocation" 
              value
    ="/WEB-INF/applicationContext.xml"/>
     
    </plug-in>
    </struts-config>


    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:util
    ="http://www.springframework.org/schema/util"
        xmlns:p
    ="http://www.springframework.org/schema/p"
        xmlns:aop
    ="http://www.springframework.org/schema/aop"
        xmlns:tx
    ="http://www.springframework.org/schema/tx"
        xsi:schemaLocation
    ="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
    >
        
        
    <bean id="userDaoImpl" class="com.yz.dao.impl.IuserDaoImpl"></bean>
    <!-- action里面有一個(gè)ius(service的接口引用),
        他來(lái)自  com.yz.services.impl.IUserServicesImpl(指向接口的實(shí)現(xiàn)類)
        IUserServicesImpl里面有一個(gè)iud(IuserDao接口引用)
        他 ref id為 userDaoImpl(指向接口的實(shí)現(xiàn)類)   
        
    -->
        
    <bean id="ius" class="com.yz.services.impl.IUserServicesImpl">
            
    <property name="iud">
                
    <ref bean="userDaoImpl"/>
            
    </property>
        
    </bean>
    </beans>

    <!--
    整合思路:
       1、通過(guò)從 Spring 的 ActionSupport或者DispachActionSuport 類而不是 Struts 的 Action 類進(jìn)行擴(kuò)展,創(chuàng)建了一個(gè)新的 Action,
       2、使用 getWebApplicationContext() 方法獲得一個(gè) ApplicationContext。為了獲得spring容器上下文對(duì)象,從而查找一個(gè) Spring bean。

    優(yōu)缺點(diǎn):
          1、這種技術(shù)很簡(jiǎn)單并且易于理解。
          2、它將 Struts 動(dòng)作與 Spring 框架耦合在一起。如果您想替換掉 Spring,那么您必須重寫(xiě)代碼。并且,由于 Struts 動(dòng)作不在 Spring 的控制之下,所以它不能獲得 Spring AOP 的優(yōu)勢(shì)。當(dāng)使用多重獨(dú)立的 Spring 環(huán)境時(shí),這種技術(shù)可能有用,但是在大多數(shù)情況下,這種方法不如另外兩種方法合適。

    -->

    方式二:
    使用 Spring 的 DelegatingRequestProcessor 覆蓋 Struts 的 RequestProcessor
    Action
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     
    */

    package com.yz.actions.action;

    import java.lang.reflect.InvocationTargetException;
    import javax.servlet.http.*;
    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.struts.action.*;
    import org.apache.struts.actions.*;
    import org.springframework.web.struts.DispatchActionSupport;

    import com.yz.actions.form.UserForm;
    import com.yz.services.IUserServices;
    import com.yz.services.impl.IUserServicesImpl;
    import com.yz.vo.UserVo;
    //spring的action管理請(qǐng)求
    public class UserAction extends DispatchAction {
        
    //針對(duì)接口編程
        IUserServices ius;
        
        
    public IUserServices getIus() {
            
    return ius;
        }


        
    public void setIus(IUserServices ius) {
            
    this.ius = ius;
        }


        
    public ActionForward test(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response) 
    {
            UserForm userForm 
    = (UserForm) form;
            UserVo uv 
    = new UserVo();
            
    try {
                BeanUtils.copyProperties(uv, userForm);
            }
     catch (Exception e) {
                    e.printStackTrace();
            }

         
     boolean add = ius.addUsers(uv);
            System.out.println(uv.getUserName());
            String url 
    = "/error.jsp";
            
    if(add){
                url 
    = "/ok.jsp";
            }

            System.out.println(url);
            
    return new ActionForward(url) ;
        }

    }

    繼承自spring的請(qǐng)求處理器替換帶哦struts的請(qǐng)求處理器,順便處理編碼問(wèn)題。
    package com.yz.myReq;

    import java.io.UnsupportedEncodingException;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.springframework.web.struts.DelegatingRequestProcessor;
    //spring的請(qǐng)求處理器

    public class mySpringRequestProccessor extends DelegatingRequestProcessor {

        @Override
        
    protected boolean processPreprocess(HttpServletRequest req,
                HttpServletResponse resp) 
    {
            
    try {
                req.setCharacterEncoding(
    "utf-8");
            }
     catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            resp.setCharacterEncoding(
    "utf-8");
            
    return super.processPreprocess(req, resp);
        }

    }

    struts-config.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

    <struts-config>
      
    <data-sources />
      
    <form-beans >
        
    <form-bean name="userForm" type="com.yz.actions.form.UserForm" />

      
    </form-beans>

      
    <global-exceptions />
      
    <global-forwards />
      
    <action-mappings >
        
    <action
          
    attribute="userForm"
          name
    ="userForm"
          path
    ="/user"
          scope
    ="request"
          type
    ="com.yz.actions.action.UserAction" 
          parameter
    ="actions"
       
    />
      
    </action-mappings>
      
    <!-- spring的請(qǐng)求處理器 自己寫(xiě)的類繼承他,加了中文亂碼處理 
      這樣在actionservlet得到action的時(shí)候,就是從spring容器中去
          獲取name和path匹配的對(duì)象 
    -->
      
    <controller processorClass="com.yz.myReq.mySpringRequestProccessor"></controller>
      <message-resources parameter="com.yz.actions.ApplicationResources" />
     
    <!-- 添加一個(gè)插件,用來(lái)在啟動(dòng)struts框架的時(shí)候,
          同時(shí)讀取spring配置文件,啟動(dòng)spring容器 
    -->
      
    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
              
    <set-property property="contextConfigLocation" 
              value
    ="/WEB-INF/applicationContext.xml"/>
     
    </plug-in>
    </struts-config>

    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:util
    ="http://www.springframework.org/schema/util"
        xmlns:p
    ="http://www.springframework.org/schema/p"
        xmlns:aop
    ="http://www.springframework.org/schema/aop"
        xmlns:tx
    ="http://www.springframework.org/schema/tx"
        xsi:schemaLocation
    ="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
    >
        
        
    <bean id="userDaoImpl" class="com.yz.dao.impl.IuserDaoImpl"></bean>
    <!-- action里面有一個(gè)ius(service的接口引用),
        他來(lái)自  com.yz.services.impl.IUserServicesImpl(指向接口的實(shí)現(xiàn)類)
        IUserServicesImpl里面有一個(gè)iud(IuserDao接口引用)
        他 ref id為 userDaoImpl(指向接口的實(shí)現(xiàn)類)  /user 
        
    -->
        
    <bean id="intfUs" class="com.yz.services.impl.IUserServicesImpl">
            
    <property name="iud">
                
    <ref bean="userDaoImpl"/>
            
    </property>
        
    </bean>
        
        
    <!-- name必須和action的path一致 -->
        <bean name="/user" class="com.yz.actions.action.UserAction">
            <property name="ius" ref="intfUs"></property>
        
    </bean>
    </beans>

    <!--
        整合思路:
           DelegatingRequestProcessor替換struts的請(qǐng)求處理器,根據(jù)struts配置的action中path屬性與applicationContext.xml中配置的bean的name屬性找到相應(yīng)的action。
         優(yōu)缺點(diǎn):
            1、這種設(shè)計(jì)使 Struts 動(dòng)作并不知道它正被 Spring 管理,并且使您能夠利用 Sping 的動(dòng)作管理框架的所有優(yōu)點(diǎn)。由于您的 Struts 動(dòng)作注意不到 Spring 的存在,所以您不需要重寫(xiě)您的 Struts 代碼就可以使用其他控制反轉(zhuǎn)容器來(lái)替換掉 Spring。
            2、DelegatingRequestProcessor 方法的確比第一種方法好,但是仍然存在一些問(wèn)題。如果您使用一個(gè)不同的 RequestProcessor,則需要手動(dòng)整合 Spring 的 DelegatingRequestProcessor。添加的代碼會(huì)造成維護(hù)的麻煩并且將來(lái)會(huì)降低您的應(yīng)用程序的靈活性。此外,還有過(guò)一些使用一系列命令來(lái)代替 Struts RequestProcessor 的傳聞。 這種改變將會(huì)對(duì)這種解決方法的使用壽命造成負(fù)面的影響。


    -->

    方式三:將 Struts Action 管理委托給 Spring 框架,使用代理
    Action

    package com.yz.actions.action;

    import java.lang.reflect.InvocationTargetException;
    import javax.servlet.http.*;
    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.struts.action.*;
    import org.apache.struts.actions.*;
    import org.springframework.web.struts.DispatchActionSupport;

    import com.yz.actions.form.UserForm;
    import com.yz.services.IUserServices;
    import com.yz.services.impl.IUserServicesImpl;
    import com.yz.vo.UserVo;
    //spring的action管理請(qǐng)求
    public class UserAction extends DispatchAction {
        
    //針對(duì)接口編程
        IUserServices ius;
        
        
    public IUserServices getIus() {
            
    return ius;
        }


        
    public void setIus(IUserServices ius) {
            
    this.ius = ius;
        }


        
    public ActionForward test(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response) 
    {
            UserForm userForm 
    = (UserForm) form;
            UserVo uv 
    = new UserVo();
            
    try {
                BeanUtils.copyProperties(uv, userForm);
            }
     catch (Exception e) {
                    e.printStackTrace();
            }

            
    boolean add = ius.addUsers(uv);
            System.out.println(uv.getUserName());
            String url 
    = "/error.jsp";
            
    if(add){
                url 
    = "/ok.jsp";
            }

            System.out.println(url);
            
    return new ActionForward(url) ;
        }

    }

    代理
    package com.yz.myActionProxy;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.springframework.web.struts.DelegatingActionProxy;


    public class mySpringActionProxy extends DelegatingActionProxy {

        @Override
        
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                ServletRequest req, ServletResponse resp) 
    throws Exception {
            HttpServletRequest request 
    = (HttpServletRequest) req;
            HttpServletResponse response 
    = (HttpServletResponse) resp;
            request.setCharacterEncoding(
    "utf-8");
            response.setCharacterEncoding(
    "utf-8");
            
    return super.execute(mapping, form, request, response);
        }

    }

    struts-config.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

    <struts-config>
      
    <data-sources />
      
    <form-beans >
        
    <form-bean name="userForm" type="com.yz.actions.form.UserForm" />

      
    </form-beans>

      
    <global-exceptions />
      
    <global-forwards />
      
    <action-mappings >
        
    <action
          
    attribute="userForm"
          name
    ="userForm"
          path
    ="/user"
          scope
    ="request"
          type="com.yz.myActionProxy.mySpringActionProxy" 

          parameter
    ="actions"
       
    />
      
    </action-mappings>
        
    <controller processorClass="com.yz.myReq.myReq"></controller>
      
    <message-resources parameter="com.yz.actions.ApplicationResources" />
     
    <!-- 添加一個(gè)插件,用來(lái)在啟動(dòng)struts框架的時(shí)候,
          同時(shí)讀取spring配置文件,啟動(dòng)spring容器 
    -->
      
    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
              
    <set-property property="contextConfigLocation" 
              value
    ="/WEB-INF/applicationContext.xml"/>
     
    </plug-in>
    </struts-config>


    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:util
    ="http://www.springframework.org/schema/util"
        xmlns:p
    ="http://www.springframework.org/schema/p"
        xmlns:aop
    ="http://www.springframework.org/schema/aop"
        xmlns:tx
    ="http://www.springframework.org/schema/tx"
        xsi:schemaLocation
    ="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
    >
        
        
    <bean id="userDaoImpl" class="com.yz.dao.impl.IuserDaoImpl"></bean>
    <!-- action里面有一個(gè)ius(service的接口引用),
        他來(lái)自  com.yz.services.impl.IUserServicesImpl(指向接口的實(shí)現(xiàn)類)
        IUserServicesImpl里面有一個(gè)iud(IuserDao接口引用)
        他 ref id為 userDaoImpl(指向接口的實(shí)現(xiàn)類)  /user 
        
    -->
        
    <bean id="intfUs" class="com.yz.services.impl.IUserServicesImpl">
            
    <property name="iud">
                
    <ref bean="userDaoImpl"/>
            
    </property>
        
    </bean>
        
        
    <!-- name必須和action的path一致 -->
        <bean name="/user" class="com.yz.actions.action.UserAction">

            
    <property name="ius" ref="intfUs"></property>
        
    </bean>
    </beans>

    <!--
        整合思路:
          1、 將 Strut 動(dòng)作管理委托給 Spring。
          2、您可以通過(guò)在 struts-config 動(dòng)作映射中注冊(cè)一個(gè)代理來(lái)實(shí)現(xiàn)。代理負(fù)責(zé)在 Spring 環(huán)境中查找 Struts 動(dòng)作。由于動(dòng)作在 Spring 的控制之下,所以它可以填充動(dòng)作的 JavaBean 屬性,并為應(yīng)用諸如 Spring 的 AOP 攔截器之類的特性帶來(lái)了可能。 
         優(yōu)缺點(diǎn): 
            1、動(dòng)作委托解決方法是這三種方法中最好的。  Struts 動(dòng)作不了解 Spring,不對(duì)代碼作任何改變就可用于非 Spring 應(yīng)用程序中。RequestProcessor 的改變不會(huì)影響它,并且它可以利用 Spring AOP 特性的優(yōu)點(diǎn)。 
            2、動(dòng)作委托的優(yōu)點(diǎn)不止如此。一旦讓 Spring 控制您的 Struts 動(dòng)作,您就可以使用 Spring 給動(dòng)作補(bǔ)充更強(qiáng)的活力。例如,沒(méi)有 Spring 的話,所有的 Struts 動(dòng)作都必須是線程安全的。如果您設(shè)置 標(biāo)記的 singleton 屬性為“false”,那么不管用何種方法,您的應(yīng)用程序都將在每一個(gè)請(qǐng)求上有一個(gè)新生成的動(dòng)作對(duì)象。您可能不需要這種特性,但是把它放在您的工具箱中也 很好。您也可以利用 Spring 的生命周期方法。例如,當(dāng)實(shí)例化 Struts 動(dòng)作時(shí), 標(biāo)記的 init-method 屬性被用于運(yùn)行一個(gè)方法。類似地,在從容器中刪除 bean 之前,destroy-method 屬性執(zhí)行一個(gè)方法。這些方法是管理昂貴對(duì)象的好辦法,它們以一種與 Servlet 生命周期相同的方式進(jìn)行管理
    -->

    Feedback

    # re: 小毅總結(jié)之--->spring整合struts的三種方式  回復(fù)  更多評(píng)論   

    2008-11-07 09:45 by 頂!
    頂!

    posts - 0, comments - 21, trackbacks - 0, articles - 101

    Copyright © H2O

    主站蜘蛛池模板: 自怕偷自怕亚洲精品| 麻豆精品国产免费观看| a视频在线观看免费| 无码人妻精品中文字幕免费 | 99免费在线观看视频| 免费黄色小视频网站| 亚洲爆乳无码专区www| 国产午夜不卡AV免费| 色吊丝最新永久免费观看网站 | 亚洲视频手机在线| 日韩在线播放全免费| 亚洲精品国自产拍在线观看| 亚洲国产精品久久66| 国产成人精品亚洲| 最近的中文字幕大全免费8| 国产成人免费网站在线观看| 久久久亚洲欧洲日产国码aⅴ| 黄网站色视频免费看无下截| 久热中文字幕在线精品免费| 亚洲视频在线观看2018| 国产99在线|亚洲| 看全色黄大色大片免费久久| 亚洲aⅴ天堂av天堂无码麻豆| 国产精品视频免费| 亚洲AV无码日韩AV无码导航| 色欲aⅴ亚洲情无码AV| 欧美a级成人网站免费| 亚洲综合久久综合激情久久| 亚洲免费在线视频观看| 国产AV无码专区亚洲AV蜜芽| 在线观看亚洲天天一三视| 朝桐光亚洲专区在线中文字幕| 亚洲国产精品无码久久九九| 亚洲成a人无码亚洲成av无码| 亚洲国产一成久久精品国产成人综合| 两性色午夜免费视频| 黑人大战亚洲人精品一区| 99re6热视频精品免费观看| 亚洲高清毛片一区二区| 亚洲熟妇av一区二区三区 | 无码天堂va亚洲va在线va|