成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何集成Struts和Spring

開發(fā) 后端
本文想通過一個(gè)簡單的實(shí)例闡述如何集成Struts和Spring。

1.Struts和Spring

Struts 代表了MVC第二類架構(gòu)的實(shí)現(xiàn),在Struts中最重要的組件是ActionServlet, Action,和 ActionForm 子類,ActionServlet 代表controller,他基于配置文件接受請(qǐng)求和把這些請(qǐng)求轉(zhuǎn)發(fā)到相應(yīng)的ActionForm和Action子類。 ActionForm把用戶輸入的數(shù)據(jù)傳送到Action, Action調(diào)用商務(wù)層組件完成必要的操作,最后提交到view。ActionServlet使用一個(gè)配置文件(struts-config.xml)加載Action子類的定義用以接受用戶請(qǐng)求,基于請(qǐng)求URL, controller 找到一個(gè)action定義去接受這個(gè)請(qǐng)求, Struts構(gòu)件處理用戶請(qǐng)求, 檢查配置文件, 完成相應(yīng)的動(dòng)作。

Spring是一種輕量級(jí)的容器,它使得使用一個(gè)外部XML配置文件非常容易綁定對(duì)象,每個(gè)對(duì)象能夠通過列出JavaBean屬性得到一個(gè)依賴對(duì)象的指針,通過綁定XML配置文件使剩下的工作更加簡單。依賴注入(DI)是非常強(qiáng)大的功能,Spring支持可插拔的事務(wù)管理器,提供事物管理方式更多的選擇. 它集成了持久性的架構(gòu)同時(shí)也提供了一個(gè)統(tǒng)一的exception 分類,Spring也提供面向方面(AOP)編程的簡單機(jī)制。

2.集成Struts和Spring

將Struts應(yīng)用集成到Spring框架可以采用多種方法,首先Spring明顯地被設(shè)計(jì)用于解決JEE的現(xiàn)實(shí)問題,如復(fù)雜性,性能低下,可測試性及其他;第二,Spring框架包括一個(gè)AOP實(shí)現(xiàn)讓你可以使用面向方面的程序設(shè)計(jì)技術(shù);第三, Spring 框架可以能夠非常容易地管理和協(xié)調(diào)Struts;和Struts類似, Spring也包含MVC 實(shí)現(xiàn).,兩個(gè)架構(gòu)都有優(yōu)缺點(diǎn),Struts是MVC最重要的架構(gòu),很多開發(fā)團(tuán)隊(duì)學(xué)會(huì)了依靠Struts在規(guī)定時(shí)限內(nèi)開發(fā)出高質(zhì)量的軟件,因此開發(fā)團(tuán)隊(duì)寧愿集成Spring的功能也不愿意轉(zhuǎn)到Spring MVC;好消息是Spring的結(jié)構(gòu)允許你集成Struts Web 框架、基于Spring的業(yè)務(wù)層和持久層,我們的方法是應(yīng)用Spring中的ActionSupport類去集成Struts,以完成集成Struts和Spring。

3.加載應(yīng)用的context

首先我們需要使用Spring中的ContextLoaderPlugin為Struts ActionServlet去裝載Spring應(yīng)用的上下文,簡單在struts-config.xml 文件中增加plug-in,如下(1)所示:

  1. <?xml version="1.0" encoding="ISO-8859-1" ?> 
  2. <!DOCTYPE struts-config PUBLIC"-//Apache Software Foundation// DTD Struts                              Configuration 1.1//EN""http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> 
  3. <struts-config> 
  4.  <form-beans> 
  5.     <form-bean name="searchForm" 
  6.                 type="org.apache.struts.validator.DynaValidatorForm"> 
  7.                <form-property name="cardno"    type="java.lang.String"/> 
  8.     </form-bean> 
  9.   </form-beans> 
  10.  <global-forwards type="org.apache.struts.action.ActionForward"> 
  11.      <forward   name="welcome"          path="/welcome.do"/> 
  12.      <forward   name="searchEntry"       path="/searchEntry.do"/> 
  13.      <forward   name="searchSubmit"      path="/searchSubmit.do"/> 
  14.  </global-forwards> 
  15.  <action-mappings> 
  16.     <action    path="/welcome" forward="/WEB-INF/pages/welcome.htm"/> 
  17.     <action    path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/> 
  18.     <action    path="/searchSubmit" 
  19.                type=" com.infotek.Creditcard.actions.SearchSubmit" 
  20.                input="/searchEntry.do" 
  21.                validate="true" 
  22.                name="searchForm"> 
  23.               <forward name="success" path="/WEB-INF/pages/detail.jsp"/> 
  24.              <forward name="failure" path="/WEB-INF/pages/search.jsp"/> 
  25.     </action>    
  26.  </action-mappings> 
  27.  <message-resources parameter="ApplicationResources"/> 
  28.  <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> 
  29.     <set-property property="pathnames" value="/WEB-INF/                                                                               validator-rules.xml,/WEB-INF/validation.xml"/> 
  30.  </plug-in> 
  31.  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">  (1)  
  32.     <set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/> 
  33.  </plug-in> 
  34. </struts-config>  

4.使用Spring的ActionSupport類

要用Spring去集成Struts,創(chuàng)建一個(gè)Spring 上下文是必須要做的。 org.springframework.web.struts.ActionSupport 類提供一個(gè) getWebApplicationContext() 方法非常容易地獲得Spring上下文,全部你需要去做的是從Spring的ActionSupport 代替Struts 中的Action類去延伸你的action,如下所示:

  1. package com.infotek.Creditcard.actions;  
  2. import java.io.IOException;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.HttpServletRequest;  
  5. import javax.servlet.http.HttpServletResponse;  
  6. import org.apache.struts.action.ActionError;  
  7. import org.apache.struts.action.ActionErrors;  
  8. import org.apache.struts.action.ActionForm;  
  9. import org.apache.struts.action.ActionForward;  
  10. import org.apache.struts.action.ActionMapping;  
  11. import org.apache.struts.action.DynaActionForm;  
  12. import org.springframework.context.ApplicationContext;  
  13. import org.springframework.web.struts.ActionSupport;  
  14. import com. infotek.Creditcard.beans.Creditcard;  
  15. import com. infotek.Creditcard.business.CreditcardService;  
  16. public class SearchSubmit extends ActionSupport { |(1)  
  17. public ActionForward execute(ActionMapping mapping,ActionForm form,  
  18. HttpServletRequest request,HttpServletResponse response)  
  19. throws IOException, ServletException {  
  20. DynaActionForm searchForm = (DynaActionForm) form;  
  21. String isbn = (String) searchForm.get("cardno");  
  22. //the old fashion way  
  23. //CreditcardService creditcardService = new CreditcardServiceImpl();  
  24. ApplicationContext ctx = getWebApplicationContext(); |(2)  
  25. CreditcardService creditcardService =  
  26. (CreditcardService ) ctx.getBean("creditcardService"); |(3)  
  27. CreditCard creditard = CreditCardService.read(cardno.trim());  
  28. if (null == creditard) {  
  29. ActionErrors errors = new ActionErrors();  
  30. errors.add(ActionErrors.GLOBAL_ERROR,new ActionError ("message.notfound"));  
  31. saveErrors(request, errors);  
  32. return mapping.findForward("failure") ;  
  33. }  
  34. request.setAttribute("creditcard", creditcard);  
  35. return mapping.findForward("success");  
  36. }  

在(1)中,我們 通過延伸Spring ActionSupport 類而不是Struts Action 類創(chuàng)建了一個(gè)action;在(2)中,我們使用getWebApplicationContext()方法獲得一個(gè) ApplicationContext;為了獲得商務(wù)服務(wù), 在(3)中,我們使用ApplicationContext去查找Spring bean;這個(gè)技術(shù)非常容易理解,不幸的是它把Struts的action和Spring framework綁定了,如果你想替換Spring你不得不重寫代碼,而且Struts的action不在Spring的控制之下, 遺憾的是這種方法無法獲得Spring AOP的好處。

5.結(jié)論

本文闡述如何集成Struts和Spring,我們嘗試使用Spring的 ActionSupport,ContextLoaderPlugIn去集成Struts,這是一種最高效的和最簡單的方式,另外還可用Spring中的代理子類去代理Struts中的RequestProcessor和代理Struts的actions。

【編輯推薦】

  1. Spring框架的7個(gè)模塊
  2. 簡述Spring JDBC的學(xué)習(xí)
  3. Spring 2.0和Spring AOP
  4. 整合Struts和Spring
  5. Spring2.0升級(jí)Spring2.0.7的變化
責(zé)任編輯:佚名 來源: IT168
相關(guān)推薦

2009-03-24 10:30:35

SpringStruts集成

2009-06-18 15:56:49

Struts和Spri

2009-06-26 17:15:44

Struts2

2009-06-30 17:03:49

Spring集成Str

2009-06-19 10:00:37

Struts和Spri

2009-06-19 15:28:03

SpringHibernate

2024-01-16 08:17:29

Mybatis驗(yàn)證業(yè)務(wù)

2009-09-24 09:18:18

2009-06-23 13:21:26

JSF和Spring

2009-06-03 09:16:03

Hibernate工作原理使用

2020-07-14 11:00:12

Spring BootRedisJava

2009-09-22 14:46:18

struts-spri

2020-09-02 17:28:26

Spring Boot Redis集成

2009-06-19 17:05:08

MVC框架Struts和Spri

2009-09-14 19:31:36

Struts1和Str

2021-12-28 11:13:05

安全認(rèn)證 Spring Boot

2011-05-13 09:53:02

strutsAjax

2009-09-25 17:47:35

Struts+Hibe

2009-07-29 09:54:34

struts2和str

2009-06-24 17:43:24

Struts和JSF
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 电影午夜精品一区二区三区 | 亚洲精品久久久久中文字幕欢迎你 | 成人免费视频 | 色妞av| 亚洲一区二区久久 | 成人在线视频网 | 精品国产乱码一区二区三区 | 亚洲精品一 | 欧美高清视频 | 91亚洲国产成人精品一区二三 | 天天操天天干天天曰 | 亚洲福利av| 91精品在线看 | 久久这里有精品 | 亚洲免费一区二区 | 国产成人福利视频 | 亚洲欧美高清 | 国产精品99久久久久久动医院 | 亚洲在线一区 | 国产二区视频 | 午夜天堂精品久久久久 | 亚洲精品电影 | 午夜天堂精品久久久久 | 国产区久久 | 黄色大片免费网站 | 中文字幕日韩一区 | 在线一区视频 | 中文字幕第7页 | 久久精品国产免费看久久精品 | 免费黄色大片 | 国产精品免费在线 | 久久久免费 | 黄色一级大片在线免费看产 | 亚洲 中文 欧美 日韩 在线观看 | 国产视频中文字幕在线观看 | 99re视频在线观看 | 91综合网 | 高清视频一区二区三区 | 欧美一区二区三区视频在线 | av天天操| 国产在线观看一区二区 |