一、spring容器的創建方式
1.直接在web.xml中配置創建spring容器
1.1 用ServletContextListener方式實現
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果有多個配置文件需要載入,使用<context-param>元素,ContextLoaderListener加載時,會查找名為contextConfigLocation的參數,所以,有下面:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value>
</context-param>
1.2 用load-on-startup Servlet方式實現
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
2.用struts plugin創建spring容器
在struts-config.xml中加入
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml(用于配置表示層上下文),/WEB-INF/applicationContext.xml"/>
</plug-in>
二.問題
如何讓控制器訪問到spring容器中的業務邏輯組件?有兩種方式2.1,2.2
2.1 spring管理控制器,并利用IOC為控制器注入邏輯組件
2.1.1 使用spring的DelegatingRequestProcessor
在struts-config.xml中加入
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
而后,struts會接取用戶請求轉發到spring context下的bean,根據bean的name來匹配,因此在struts的action中配置type是沒用的!
如下:
<action path="/login" name="loginForm" scope="requerst" validate="true" input="/login.jsp">
<forward name="input" path="/login.jsp"/>
<forward name="welcom" path="/welcom.jsp"/>
</action>
然后在web.xml中配置:
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.1.2 使用DelegatingActionProxy(為了避免應用程序本身就擴展了RequetstProcessor,DelegatingRequestProcessor就用不成了)
DelegatingActionProxy被配置成struts的Action(即所有請求先被ActionServlet得到),轉發到相應的Action,而Action的實現全都是DelegatingActionProxy,DelegatingActionProxy再把請求轉發給spring容器的Action.
DelegatingActionProxy不需要在struts-config.xml中配置<controller>元素,只需將action type進行如下配置:
<action path="/login" name="loginForm" scope="requerst" validate="true" input="/login.jsp" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="input" path="/login.jsp"/>
<forward name="welcom" path="/welcom.jsp"/>
</action>
2.2 使用srping的ActionSupport代替struts 的Action
這種方式下,struts的Action不受spring Ioc容器管理,Action代碼與spring API部分耦合(造成代碼污染),但其增強了代碼的可讀性,Action代碼中顯示調用業務邏輯組件,無需等容器注入。
例如:
public class LoginAction extends ActionSupport { //繼承spring的ActionSupport
public LoginAction()
{
//不可在構造方法中調用getWebApplicationContext()
}
private LoginService loginService; //將業務邏輯組件對象作為成員變量
public LoginService getLoginService()
{
return (LoginService)getWebApplicationContext().getBean("loginService");
}
//重寫execute()方法
}
struts-config.xml中<action>元素正常配置
http://www.aygfsteel.com/DenisLing/archive/2005/11/21/20779.html
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
27 | 28 | 29 | 30 | 1 | 2 | 3 | |||
4 | 5 | 6 | 7 | 8 | 9 | 10 | |||
11 | 12 | 13 | 14 | 15 | 16 | 17 | |||
18 | 19 | 20 | 21 | 22 | 23 | 24 | |||
25 | 26 | 27 | 28 | 29 | 30 | 31 | |||
1 | 2 | 3 | 4 | 5 | 6 | 7 |
長春語林科技歡迎您!
常用鏈接
留言簿(6)
隨筆分類
- ajax(2)
- android(5)
- css(2)
- db2(2)
- docker(10)
- flex(22)
- hibernate(16)
- html5(9)
- java(12)
- java8(8)
- jquery(4)
- js(30)
- jsp(2)
- jstl(3)
- linux(14)
- mongodb(1)
- mui(1)
- mysql(14)
- oracle(3)
- spring(8)
- sqlserver(4)
- struts(9)
- struts2(13)
- tomcat(6)
- UML(1)
- util(50)
- vue(1)
- weblogic(1)
隨筆檔案
- 2020年4月 (1)
- 2020年3月 (1)
- 2020年2月 (2)
- 2019年10月 (2)
- 2019年9月 (1)
- 2019年7月 (1)
- 2019年4月 (1)
- 2019年1月 (1)
- 2018年12月 (2)
- 2018年8月 (1)
- 2018年6月 (3)
- 2018年5月 (9)
- 2018年3月 (9)
- 2017年12月 (1)
- 2017年10月 (1)
- 2017年7月 (1)
- 2017年6月 (1)
- 2017年5月 (1)
- 2017年3月 (3)
- 2017年2月 (2)
- 2017年1月 (1)
- 2016年12月 (1)
- 2016年11月 (1)
- 2016年9月 (1)
- 2016年4月 (3)
- 2016年3月 (2)
- 2015年8月 (5)
- 2015年3月 (1)
- 2014年8月 (1)
- 2012年11月 (1)
- 2012年5月 (2)
- 2012年4月 (5)
- 2011年12月 (1)
- 2011年10月 (3)
- 2011年9月 (2)
- 2011年8月 (10)
- 2011年7月 (3)
- 2011年6月 (4)
- 2011年5月 (2)
- 2011年4月 (3)
- 2011年3月 (12)
- 2011年1月 (2)
- 2010年12月 (1)
- 2010年9月 (2)
- 2010年8月 (4)
- 2010年6月 (1)
- 2010年4月 (1)
- 2010年3月 (1)
- 2009年11月 (1)
- 2009年9月 (2)
- 2009年8月 (1)
- 2009年7月 (2)
- 2009年6月 (1)
- 2009年5月 (3)
- 2009年4月 (8)
- 2009年3月 (5)
- 2009年2月 (4)
- 2009年1月 (2)
- 2008年12月 (10)
- 2008年11月 (2)
- 2008年9月 (10)
- 2008年8月 (12)
- 2008年7月 (12)
- 2008年6月 (3)
- 2008年5月 (53)
文章分類
文章檔案
相冊
收藏夾
搜索
最新評論

- 1.?re: js 操作文件[未登錄]
- 00
- --00
- 2.?re: s:bean.jsp
- fdfdsa
- --dfasdf
- 3.?re: hibernate 常用標識生成器
- 藝達廣告歡迎您
- --藝達廣告
- 4.?re: linux mod_jk.so 下載[未登錄]
- 3Q!
- --me
- 5.?re: weblogic參數設置[未登錄]
- 垃圾
- --xx