Struts2+Spring集成合並


前邊單獨總結了Struts2springIbaits框架了,那么怎么結合使用呢?這次先來看一下Sturts2Spring的集成合並。其實挺簡單的,就是導入各自的jar包以及連接彼此的jar包,分好彼此的工作就可以了。

 

    好看一下Struts2+Spring的集成方案!

 

          Struts2Spring集成有兩種方案,是根據action的創建來划分的!

 

          方案一,Struts2負責流程,Spring負責對象的創建;ActionStruts2框架負責創建;ServiceSpring框架負責創建。看一下其實現的一個例子的步驟:

 

         1,集成Struts2框架到項目中,搭建Struts2框架環境,這個可以看我們的Struts2()——總體介紹,步驟和那個是一樣的。

 

         2,集成Spring框架到項目中,搭建Spring框架環境,這里也可以看一下Spring()——總體介紹。但是這里我想說一下,導入我們的jar包,和拷貝好兩個配置文件后,為了是我們項目在加載時就初始化解析applicationContext.xml,並創建相應的各種對象。我們在web.xml中可以添加框架的監聽器:

 

 

[html]  view plain  copy
 
 
 
 
  在CODE上查看代碼片派生到我的代碼片
  1. <span style="font-size:18px;">  <!-- 用於指定Spring的配置文件路徑 -->  
  2.     <context-param>  
  3.         <param-name>contextConfigLocation</param-name>  
  4.         <param-value>classpath:applicationContext.xml</param-value>  
  5.     </context-param>  
  6.    
  7.     <!-- 服務器啟動時,通過監聽器初始化Spring的配置環境   
  8.         監聽器,默認加載文件是:/WEB-INF/applicationContext.xml  
  9.     -->  
  10.     <listener>  
  11.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  12.   
  13. </listener></span>  

 

 


            3,這樣兩者的各自環境就算搭建好了,那么如何進行兩者的集成合並呢?這里Struts2為我們提供了jar:struts2-spring-plugin-2.1.8.1.jar。我們需要將此jar包也引用到項目中。這樣兩個框架就可以通過此jar包進行合並集成了。

 

           簡單說一下這個jar包的作用吧:插件中提供了新的工廠類,覆蓋了Struts2的原工廠類。新工廠類的工作原理:首先根據Action的完整類名,到Spring配置文件中查詢bean標簽的id是否存在一致的名稱。如果有一致的說明,Action對象是由Spring負責創建,並有Spring進行裝配組合對象之間的關系。        如果查找不到,會進行特殊的處理操作,再由Strtus2框架進行反射創建Action對象,並采用Strust2框架的自動裝配功能來完成ActionService對象的關聯。默認是根據name進行查找的,當然我們也可以進行修改,例如改成以type類型進行查找的方式:

 

<constantname="struts.objectFactory.spring.autoWire"value="type"></constant>

 

         4,這樣環境就算搭建好了,看看我們各個配置文件中的代碼吧,重點看注釋:

 

            a,web.xml中配置了兩個框架的核心配置文件:

[html]  view plain  copy
 
 
 
 
  在CODE上查看代碼片派生到我的代碼片
  1. <span style="font-size:18px;">  <?xml version="1.0" encoding="UTF-8"?>  
  2.     <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  5.         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  6.                
  7.         <!-- 用於指定Spring的配置文件路徑 -->  
  8.         <context-param>  
  9.             <param-name>contextConfigLocation</param-name>  
  10.             <param-value>classpath:applicationContext.xml</param-value>  
  11.         </context-param>  
  12.        
  13.         <!-- 服務器啟動時,通過監聽器初始化Spring的配置環境   
  14.             監聽器,默認加載文件是:/WEB-INF/applicationContext.xml  
  15.         -->  
  16.         <listener>  
  17.             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  18.         </listener>  
  19.            
  20.         <!-- 配置Struts2框架的核心調度器 -->  
  21.         <filter>  
  22.             <filter-name>struts2</filter-name>  
  23.             <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  24.         </filter>  
  25.         <filter-mapping>  
  26.             <filter-name>struts2</filter-name>  
  27.             <url-pattern>/*</url-pattern>  
  28.         </filter-mapping>  
  29.        
  30.         <!-- 默認主界面 -->  
  31.         <welcome-file-list>  
  32.             <welcome-file>login.jsp</welcome-file>  
  33.         </welcome-file-list>  
  34.     </web-app>  
  35. </span>  

 


             b,struts2.xml,配置了action類:

 

 

[html]  view plain  copy
 
 
 
 
  在CODE上查看代碼片派生到我的代碼片
  1. <span style="font-size:18px;">  <struts>  
  2.         <!-- 指定以name的方式組合action和service的關系 -->  
  3.         <constant name="struts.objectFactory.spring.autoWire" value="name"></constant>  
  4.           
  5.         <!-- 設置action的信息 -->  
  6.         <package name="example" namespace="/user" extends="struts-default">  
  7.             <action name="login" class="com.ljh.action.LoginAction" method="login">  
  8.                 <result name="success" type="redirect">/success.jsp</result>  
  9.                 <result name="login" type="redirect">/login.jsp</result>  
  10.             </action>     
  11.         </package>          
  12.     </struts>  
  13. </span>  

           c,applicationContext.xml中配置了service類:

 

 

[html]  view plain  copy
 
 
 
 
  在CODE上查看代碼片派生到我的代碼片
  1. <span style="font-size:18px;">  <!-- service類的基本信息 -->  
  2.   
  3. <bean id="userService" class="com.ljh.service.UserService" ></bean></span>  

 

           對於actionservicejsp的代碼較簡單登錄的功能,很容易實現的。其實看完了,感覺這種集成合並的方式Struts2Spring都是各自干各自的,集成的不是很好。因為Sping是容器么,對對象的管理更為專業,Struts2對流程的控制更加專業。所以這種方式很少使用,不推薦。看我們的第二種集成方式,也是常用的集成方式。

 

 

          方案二,Struts2負責流程,Spring負責對象的創建,ActionService都由Spring框架負責創建。這是常用的集成合並方案。步驟和上邊的基本上一樣,都是導入響應的jar包,拷入響應的配置文件,web.xml文件的寫法也一樣。主要看一下兩個框架核心配置文件的和第一種方案的寫法區別:

 

         1struts2.xml的寫法:

[html]  view plain  copy
 
 
 
 
  在CODE上查看代碼片派生到我的代碼片
  1. <span style="font-size:18px;"><struts>  
  2.          <!--寫法基本一樣但是注意class的值,這里沒有寫真正路徑,為了是在Spring中根據此值進行查找-->  
  3.     <package name="example" namespace="/user" extends="struts-default">  
  4.         <action name="login" class="loginAction" method="login">  
  5.             <result name="success" type="redirect">/success.jsp</result>  
  6.             <result name="login" type="redirect">/login.jsp</result>  
  7.         </action>  
  8.     </package>          
  9. </struts>  
  10. </span>  

          2applicatinContext.xml的寫法:

 

 

[html]  view plain  copy
 
 
 
 
  在CODE上查看代碼片派生到我的代碼片
  1. <span style="font-size:18px;">  <!--這里的id對應上邊的class,這里的class才是真正的路徑,采用了Spring的根據name自動裝配的功能,當然也可以我們手動指定,這里需要注意的是,action需要多例創建,而Spring默認為單例創建的,所以需要制定scope="prototype"-->  
  2.     <bean id="loginAction" class="com.ljh.action.LoginAction" autowire="byName" scope="prototype"></bean>  
  3.    
  4.     <bean id="userService" class="com.ljh.service.UserService" ></bean>  
  5. </span>  

 

 


          這樣就實現了二者的合並了,主要是將action交給Spring創建,並組合彼此的關系了,是彼此框架只干自己的事,分工明確,提高效率。

 

        綜上,為Struts2Spring的簡單合並總結,例子較為簡單,還需要在實踐中體會,靈活應用框架才會是我們的開發變的更為高效,更為簡單。通過編程,通過框架仔細琢磨其中的魅力,用心鑽研,才會有更大的收獲。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM