spring struts 無縫整合方式

2021-07-28 23:09:35 字數 1326 閱讀 2592

step1:依賴和基本常識

1. 首先要新增整合的核心jar包:struts2-spring-plugin-2.3.16.3.jar。

2. 按照名稱匹配的原則,定義業務bean和action中的setter方法,注意呼叫的是set方法而不是屬性,但是一般命名一致便於閱讀。

3. struts.xml中按照正常配置action。

step2:web.xml檔案配置

param>

contextconfiglocationparam-name>

param>

org.springframework.web.context.contextloaderlistenerlistener-class>

listener>

opensessioninviewfilterfilter-name>

org.springframework.orm.hibernate3.support.opensessioninviewfilterfilter-class>

filter>

opensessioninviewfilterfilter-name>

*.actionurl-pattern>

上面***對事務開關影響如下圖(藍色和黃色線條分別為加和不加的開閉位置)。

- 4.當然struts2的過濾器也需要配置,這不是本文重點。故截圖示意。

到這裡spring + struts 整合方式檔案配置ok。

但是spring預設在按name(預設)或按type注入的,靈活性不足。如:同乙個業務bean用在不同的業務處理過程中,對dao和其他引數的需求不一樣,dao和引數的注入就不一樣。這就需要在spring中就要宣告2個業務bean,這樣無論bytype還是byname都不適用。如下圖:

那麼就需要把action直接宣告在spring中,如下圖:

Spring,Struts整合方式

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

spring struts整合 兩種方案

spring struts整合 第一種方案 原理 在action中取得beanfactory物件,然後通過beanfactory獲取業務邏輯物件 總結,從action上來看這種方案存在的缺點,它產生了依賴,loginaction如果離開spring,loginaction依賴了beanfactory...

Spring Struts整合的四種方式

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