自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實例說明Struts和Spring如何集成

開發(fā) 后端
本文用實例說明Struts和Spring如何集成,包括加載應用的context和使用Spring的ActionSupport類。

本文想通過一個簡單的實例闡述如何集成Struts和Spring。

1.Struts和Spring

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

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

2.Struts和Spring的集成

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

3.加載應用的context

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

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

4.使用Spring的ActionSupport類

要用Spring去集成Struts,創(chuàng)建一個Spring 上下文是必須要做的。 org.springframework.web.struts.ActionSupport 類提供一個 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)建了一個action;在(2)中,我們使用getWebApplicationContext()方法獲得一個 ApplicationContext;為了獲得商務服務, 在(3)中,我們使用ApplicationContext去查找Spring bean;這個技術非常容易理解,不幸的是它把Struts的action和Spring framework綁定了,如果你想替換Spring你不得不重寫代碼,而且Struts的action不在Spring的控制之下, 遺憾的是這種方法無法獲得Spring AOP的好處。

5.結(jié)論

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

【編輯推薦】

  1. Spring AOP的一些概念
  2. acegi到Spring security的轉(zhuǎn)換方式
  3. Spring Framework的理解
  4. 解決Spring2.0向spring2.5遷移的問題
  5. Spring框架人氣暴漲
責任編輯:佚名 來源: 中國IT實驗室
相關推薦

2009-03-24 10:30:35

SpringStruts集成

2009-06-19 15:52:58

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-07-17 17:45:56

iBATIS Spri

2009-06-19 15:28:03

SpringHibernate

2009-06-01 16:18:30

SpringJPA集成

2024-01-16 08:17:29

Mybatis驗證業(yè)務

2009-09-24 09:18:18

2009-06-10 14:53:25

netbeans st實例

2009-06-05 10:46:12

struts logilogic標簽

2009-06-23 13:21:26

JSF和Spring

2009-06-03 09:16:03

Hibernate工作原理使用

2009-06-08 16:52:00

2020-07-14 11:00:12

Spring BootRedisJava

2009-09-22 14:46:18

struts-spri

2009-06-05 11:01:23

struts mvcMVC工作原理

2009-07-20 16:51:59

Struts2.0+i

2009-07-15 13:11:25

ibatis動態(tài)查詢
點贊
收藏

51CTO技術棧公眾號