spring struts整合 兩種方案

2021-04-21 02:04:51 字數 1059 閱讀 4275

spring+struts整合(第一種方案)

原理:在action中取得beanfactory物件,然後通過beanfactory獲取業務邏輯物件、

總結,從action上來看這種方案存在的缺點,它產生了依賴,loginaction如果離開spring,loginaction依賴了beanfactory查詢(依賴查詢)

不過看上去很直接,比較好理解。

spring+struts整合(第二種方案)

原理:將業務邏輯物件通過spring注入到action中,從而避免在action類中的直接**查詢

1、spring和struts的依賴庫配置、

* 配置struts

--拷貝struts類庫和jstl類庫

--修改web.xml檔案配置actionservlet

--提供struts-config.xml檔案

--提供國際化資源檔案

* 配置spring

--拷貝spring類庫

--提供spring配置檔案

2、因為action需要呼叫業務邏輯方法,所以需要在action中提供setter方法,讓spring將業務邏輯物件注入過來

3、在struts-config.xml檔案中配置action

* 標籤中的type屬性需要修改為type="org.springframework.web.struts.delegatingactionproxy"

delegatingactionproxy 是乙個action主要作用是取得beanfactory然後根據中的path屬性值到

ioc容器中取得本次請求相對應的action

4、在spring配置檔案中,需要定義struts的action,如:

* 必須使name屬性,name屬性值必須和struts-config.xml檔案中的標籤的path屬性值一致

* 必須要把業務邏輯物件注入進來

* 建議將scope設定為prototype這樣就避免了struts action的執行緒安全問題

總結,解決了依賴查詢,使用依賴注入,它沒有侵入性。

Spring,Struts整合方式

為了在struts中載入spring context,需要在struts config.xml檔案中加入如下部分 第一種方法 通過struts的plug in在struts和spring之間提供了良好的結合點。通過plug in我們實現了spring context的載入,不過僅僅載入spring ...

spring struts 無縫整合方式

step1 依賴和基本常識 1.首先要新增整合的核心jar包 struts2 spring plugin 2.3.16.3.jar。2.按照名稱匹配的原則,定義業務bean和action中的setter方法,注意呼叫的是set方法而不是屬性,但是一般命名一致便於閱讀。3.struts.xml中按照正...

Spring Struts整合的四種方式

1.在action中取得beanfactory物件,然後通過beanfactory獲取業務邏輯物件 web.xml 2 將業務邏輯物件通過spring注入到action中,從而避免了在action類中的直接 查詢,web.xml配置同上 在struts config.xml檔案中配置action 標...