Struts2 發布已經很長時間了,一直沒有顧得上學習,本周工作比較輕松,花點時間照著例子做了一下,但是在與Spring 集成的時候出現問題,action找不到Spring中定義的bean,折騰了兩個多小時才最終搞定,決定把心得記錄下來。
Struts2 集成 Spring 的 Web.xml 最簡配置
<?
xml?version="1.0"?encoding="UTF-8"
?>
<
web-app?
id
="WebApp_9"
?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"
>
????
<
display-name
>
Struts?2.0
</
display-name
>
????
????
<
filter
>
????????
<
filter-name
>
struts
</
filter-name
>
????????
????????
<
filter-class
>
org.apache.struts2.dispatcher.FilterDispatcher
</
filter-class
>
????
</
filter
>
????
<
filter-mapping
>
????????
<
filter-name
>
struts
</
filter-name
>
????????
<
url-pattern
>
/*
</
url-pattern
>
????
</
filter-mapping
>
????
????
<
listener
>
????????
<
listener-class
>
????????????org.springframework.web.context.ContextLoaderListener
????????
</
listener-class
>
????
</
listener
>
????
????
<
welcome-file-list
>
????????
<
welcome-file
>
index.html
</
welcome-file
>
????
</
welcome-file-list
>
</
web-app
>
此配置適用于將Spring 的配置文件放在與Web.xml同一目錄,即WEB-INF目錄下,且配置文件采用默認命名applicationContext.xml,如果Spring配置文件沒有放在WEB-INF下或者采用了自定義名稱,則Web.xml應該如下定義:
<?
xml?version="1.0"?encoding="UTF-8"
?>
<
web-app?
id
="WebApp_9"
?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"
>
????
<
context-param
>
????????
<
param-name
>
contextConfigLocation
</
param-name
>
????????
<
param-value
>
classpath*:applicationContext*.xml
</
param-value
>
????
</
context-param
>
????
<
filter
>
????????
<
filter-name
>
struts
</
filter-name
>
????????
????????
<
filter-class
>
org.apache.struts2.dispatcher.FilterDispatcher
</
filter-class
>
????
</
filter
>
????
<
filter-mapping
>
????????
<
filter-name
>
struts
</
filter-name
>
????????
<
url-pattern
>
/*
</
url-pattern
>
????
</
filter-mapping
>
????
????
<
listener
>
????????
<
listener-class
>
????????????org.springframework.web.context.ContextLoaderListener
????????
</
listener-class
>
????
</
listener
>
????
????
<
welcome-file-list
>
????????
<
welcome-file
>
index.html
</
welcome-file
>
????
</
welcome-file-list
>
</
web-app
>
注意這里比上個配置多出的項
表示Spring配置文件放在CLASSPATH目錄下,即WEB-INF/classes目錄下,名稱為applicationContext*.xml,其中“*”為任意字符
注意,這還沒完,CLASSPATH下有個struts.property文件,必須在里面添加一行內容:
(我就是缺了這個導致action找不到bean)
struts.objectFactory?=?spring??
<beans?default-autowire="autodetect">???

??……?……

Struts2 集成 Spring 的 Web.xml 最簡配置


























此配置適用于將Spring 的配置文件放在與Web.xml同一目錄,即WEB-INF目錄下,且配置文件采用默認命名applicationContext.xml,如果Spring配置文件沒有放在WEB-INF下或者采用了自定義名稱,則Web.xml應該如下定義:





























注意這里比上個配置多出的項
???<
context-param
>
??????< param-name > contextConfigLocation </ param-name >
??????< param-value > classpath*:applicationContext*.xml </ param-value >
???</ context-param >
??????< param-name > contextConfigLocation </ param-name >
??????< param-value > classpath*:applicationContext*.xml </ param-value >
???</ context-param >

表示Spring配置文件放在CLASSPATH目錄下,即WEB-INF/classes目錄下,名稱為applicationContext*.xml,其中“*”為任意字符
注意,這還沒完,CLASSPATH下有個struts.property文件,必須在里面添加一行內容:
(我就是缺了這個導致action找不到bean)

給Spring配置文件的<beans>元素加上如下屬性




OK,配置完畢!可以使用了