spring+hibernate+struts 整合的三種方法,我只用第三種可以。

來源:互聯網
上載者:User

 

一.用spring的DelegatingRequestProcessor替換struts的RequestProcessor.

1)不要在web.xml中設定ApplicationContext的自動載入,在struts-config.xml中通plug-in設定.

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

2)在struts-config.xml中設定RequestProcessor的替換類.

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>

3)不要在struts-config.xml中的<action>元素中設定action的type屬性.

<action path="/login" input="/index.jsp"
    validate="true" scope="request">
    <forward name="forward" path="/success.jsp"></forward>
</action>

4)在applicationContext.xml或其他spring bean設定檔中設定由DelegatingRequestProcessor轉寄的bean,這個bean就是Action類.

<bean name="/login" class="mypack.LoginAction" singleton="false">
    <property name="property1" ref="otherbean"/>
 </bean>

 

 二.使用DelegatingActionProxy,此種方法是在action中再把請求轉寄給定義在applicationContext.xml中的Action.

1)同第一種方法的1).

2)如果試了第一種方法,去掉struts-config.xml中的

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>

元素.

3)需要在struts-config.xml中定義action的type="org.springframework.web.struts.DelegatingActionProxy".即第一種方法的3)中加入type屬性.

<action path="/login" input="/index.jsp" validate="true"
    scope="request" type="org.springframework.web.struts.DelegatingActionProxy">
    <forward name="forward" path="/success.jsp"></forward>
   </action>

4)同第一種方法的4).

 

三.使用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;            //獲得  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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.