實現Spring與Struts 2整合

2021-08-08 09:35:18 字數 2155 閱讀 4497

實現spring與struts 2整合

如何實現spring與struts 2框架的整合呢?其實非常簡單,這是因為struts 2框架主要是通過外掛程式來實現對其他框架的支援。

spring框架與struts 2框架的整合過程非常簡單,因為struts 2框架中已經提供了關於spring框架的外掛程式。對於struts 2框架中所有的外掛程式,其實就是一些名稱不同的jar檔案,一般以"struts2-框架名-plugin-版本號"的方式命名。同理,關於spring框架的外掛程式也不例外。

注意:對於struts-plugin.xml和struts.xml配置檔案,struts 2框架首先載入的是struts- plugin.xml配置檔案,其次才是struts.xml配置檔案。

如何在spring和struts 2框架整合的專案中安裝關於sping框架的外掛程式呢?一般分為以下兩個步驟。

首先將關於spring框架的外掛程式複製到專案的classpath下,即先複製外掛程式的jar檔案到專案名稱\webroot\lib目錄下,然後用滑鼠右鍵單擊該檔案,在彈出的快捷選單中選擇"build path>add to build path"命令就會把該檔案轉移到classpath下,如同10.26所示。

圖10.25  struts-plugin檔案

圖10.26  新增jar檔案

xmlversion

="1.0"

encoding

="utf-8"

?>

…  <

filter

>

<

filter-name

>

struts2

filter-name

>

<

filter-class

>

org.apache.struts2.dispatcher.filterdispatcher  

filter-class

>

filter

>

<

>

<

filter-name

>

struts2

filter-name

>

<

url-pattern

>

/*url-pattern

>

>

>

配置spring框架後的內容如下:
xmlversion

="1.0"

encoding

="utf-8"

?>

…  <

filter

>

<

filter-name

>

struts2

filter-name

>

<

filter-class

>

org.apache.struts2.dispatcher.filterdispatcher  

filter-class

>

filter

>

<

>

<

filter-name

>

struts2

filter-name

>

<

url-pattern

>

/*url-pattern

>

>

<

listener

>

<

listener-class

>

org.springframework.web.context.contextloaderlistener  

listener-class

>

listener

>

…  >

注意:在沒有配置spring框架時,struts 2中的action都是在struts.xml中宣告的,但是當使用spring框架後,這些action物件的例項化過程就會由該框架實現。

Struts與Spring的整合

struts核心是mvc,struts與spring的整合就是把struts的action交給spring去管理,從而達到簡化程式的目的 一 配置spring上下文和監聽 配置spring上下文和監聽有兩種方式 方式一 web.xml web主要配置檔案 而主要用於監聽web的上下文,可用下面 代替...

Spring與Struts如何整合

struts,spring,hibernate三者也學習得差不多了,是時候該去深入了解如何去用spring來融合其他兩個 spring與struts如何整合 以下技術文件 網路 為了在struts中載入spring context,需要在struts config.xml檔案中加入如下部分 通過st...

struts與spring整合學習筆記

一 在strust config.xml中,配置struts和單獨用struts差不多,區別在 上面的例子中的type應該是實際存在的類,但是現在卻用乙個spring框架下的 類來代替。在apllicationcontext.xml中定義bean時 bean name login class net...