spring+struts整合方式

 SRPING容器自動加載STRUTS應用

一、MVC框架創建ApplicationContext實例,在MVC框架加載時自動創建SPRING容器。
1、由spring容器管理action兩種方式:
通過contextConfigLocation屬性載入,多個配置文件則用","隔開。
  1. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  2.     <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
  3. </plug-in>

·DelegatingRequestProcessor
  1. <controller>
  2.       <set-property property="processorClass"
  3.               value="org.springframework.web.struts.DelegatingRequestProcessor" />
  4.  </controller>
action配置就無須配置type屬性,即使配置了type屬性也沒用處。

·DelegatingActionProxy
修改action的type屬性爲:
type="org.springframework.web.struts.DelegatingActionProxy"

2、ActionSupport代替Action使action在程序中手動獲得
在Action中訪問ApplicationContext兩種方法:
·WebApplicationContextUtils工具類
通過ServletContext獲得spring容器實例。

·ActionSupport支持類
通過getWebApplicationContext()方法獲取ApplicationContext實例。


二、在web.xml文件中加載SPRING容器
兩種策略:
1、ServletContextListener實現
支持Servlet 2.3以及以上

·只有一個配置文件applicationContext.xml
  1. <listener>
  2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. </listener>

·有多個配置文件applicationContext.xml daoContext.xml
  1. <context-param>
  2.     <param-name>contextConfigLocation</param-name>
  3.     <param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value>
  4. </context-param>
  5. <listener>
  6.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  7. </listener>

2、load-on-startup Servlet實現
不支持Servlet 2.3
·只有一個配置文件applicationContext.xml
  1. <servlet>
  2.     <servlet-name>context</servlet-name>
  3.     <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  4.     <load-on-startup>1</load-on-startup>
  5. </servlet>

·有多個配置文件applicationContext.xml daoContext.xml
  1. <context-param>
  2.     <param-name>contextConfigLocation</param-name>
  3.     <param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value>
  4. </context-param>
  5. <servlet>
  6.     <servlet-name>context</servlet-name>
  7.     <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  8.     <load-on-startup>1</load-on-startup>
  9. </servlet>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章