前邊單獨總結了Struts2,spring和Ibaits框架了,那么怎么結合使用呢?這次先來看一下Sturts2和Spring的集成合並。其實挺簡單的,就是導入各自的jar包以及連接彼此的jar包,分好彼此的工作就可以了。
好看一下Struts2+Spring的集成方案!
Struts2和Spring集成有兩種方案,是根據action的創建來划分的!
方案一,Struts2負責流程,Spring負責對象的創建;Action由Struts2框架負責創建;Service由Spring框架負責創建。看一下其實現的一個例子的步驟:
1,集成Struts2框架到項目中,搭建Struts2框架環境,這個可以看我們的Struts2(一)——總體介紹,步驟和那個是一樣的。
2,集成Spring框架到項目中,搭建Spring框架環境,這里也可以看一下Spring(一)——總體介紹。但是這里我想說一下,導入我們的jar包,和拷貝好兩個配置文件后,為了是我們項目在加載時就初始化解析applicationContext.xml,並創建相應的各種對象。我們在web.xml中可以添加框架的監聽器:
- <span style="font-size:18px;"> <!-- 用於指定Spring的配置文件路徑 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <!-- 服務器啟動時,通過監聽器初始化Spring的配置環境
- 監聽器,默認加載文件是:/WEB-INF/applicationContext.xml
- -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </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框架的自動裝配功能來完成Action和Service對象的關聯。默認是根據name進行查找的,當然我們也可以進行修改,例如改成以type類型進行查找的方式:
<constantname="struts.objectFactory.spring.autoWire"value="type"></constant>
4,這樣環境就算搭建好了,看看我們各個配置文件中的代碼吧,重點看注釋:
a,web.xml中配置了兩個框架的核心配置文件:
- <span style="font-size:18px;"> <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <!-- 用於指定Spring的配置文件路徑 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <!-- 服務器啟動時,通過監聽器初始化Spring的配置環境
- 監聽器,默認加載文件是:/WEB-INF/applicationContext.xml
- -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 配置Struts2框架的核心調度器 -->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 默認主界面 -->
- <welcome-file-list>
- <welcome-file>login.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
- </span>
b,struts2.xml,配置了action類:
- <span style="font-size:18px;"> <struts>
- <!-- 指定以name的方式組合action和service的關系 -->
- <constant name="struts.objectFactory.spring.autoWire" value="name"></constant>
- <!-- 設置action的信息 -->
- <package name="example" namespace="/user" extends="struts-default">
- <action name="login" class="com.ljh.action.LoginAction" method="login">
- <result name="success" type="redirect">/success.jsp</result>
- <result name="login" type="redirect">/login.jsp</result>
- </action>
- </package>
- </struts>
- </span>
c,applicationContext.xml中配置了service類:
- <span style="font-size:18px;"> <!-- service類的基本信息 -->
- <bean id="userService" class="com.ljh.service.UserService" ></bean></span>
對於action、service和jsp的代碼較簡單登錄的功能,很容易實現的。其實看完了,感覺這種集成合並的方式Struts2和Spring都是各自干各自的,集成的不是很好。因為Sping是容器么,對對象的管理更為專業,Struts2對流程的控制更加專業。所以這種方式很少使用,不推薦。看我們的第二種集成方式,也是常用的集成方式。
方案二,Struts2負責流程,Spring負責對象的創建,Action和Service都由Spring框架負責創建。這是常用的集成合並方案。步驟和上邊的基本上一樣,都是導入響應的jar包,拷入響應的配置文件,web.xml文件的寫法也一樣。主要看一下兩個框架核心配置文件的和第一種方案的寫法區別:
1,struts2.xml的寫法:
- <span style="font-size:18px;"><struts>
- <!--寫法基本一樣但是注意class的值,這里沒有寫真正路徑,為了是在Spring中根據此值進行查找-->
- <package name="example" namespace="/user" extends="struts-default">
- <action name="login" class="loginAction" method="login">
- <result name="success" type="redirect">/success.jsp</result>
- <result name="login" type="redirect">/login.jsp</result>
- </action>
- </package>
- </struts>
- </span>
2,applicatinContext.xml的寫法:
- <span style="font-size:18px;"> <!--這里的id對應上邊的class,這里的class才是真正的路徑,采用了Spring的根據name自動裝配的功能,當然也可以我們手動指定,這里需要注意的是,action需要多例創建,而Spring默認為單例創建的,所以需要制定scope="prototype"-->
- <bean id="loginAction" class="com.ljh.action.LoginAction" autowire="byName" scope="prototype"></bean>
- <bean id="userService" class="com.ljh.service.UserService" ></bean>
- </span>
這樣就實現了二者的合並了,主要是將action交給Spring創建,並組合彼此的關系了,是彼此框架只干自己的事,分工明確,提高效率。
綜上,為Struts2和Spring的簡單合並總結,例子較為簡單,還需要在實踐中體會,靈活應用框架才會是我們的開發變的更為高效,更為簡單。通過編程,通過框架仔細琢磨其中的魅力,用心鑽研,才會有更大的收獲。