spring+struts的集成(第一種集成方案)
原理:在Action中取得BeanFactory對象,然后通過BeanFactory獲取業(yè)務(wù)邏輯對象
1、spring和struts依賴庫配置
?* 配置struts
??--拷貝struts類庫和jstl類庫
??--修改web.xml文件來配置ActionServlet
??--提供struts-config.xml文件
??--提供國際化資源文件
?* 配置spring
??--拷貝spring類庫
??--提供spring配置文件
??
2、在struts的Action中調(diào)用如下代碼取得BeanFactory
?BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
3、通過BeanFactory取得業(yè)務(wù)對象,調(diào)用業(yè)務(wù)邏輯方法???
??
?
spring+struts的集成(第二種集成方案)
原理:將業(yè)務(wù)邏輯對象通過spring注入到Action中,從而避免了在Action類中的直接代碼查詢
1、spring和struts依賴庫配置
?* 配置struts
??--拷貝struts類庫和jstl類庫
??--修改web.xml文件來配置ActionServlet
??--提供struts-config.xml文件
??--提供國際化資源文件
?* 配置spring
??--拷貝spring類庫
??--提供spring配置文件
2、因為Action需要調(diào)用業(yè)務(wù)邏輯方法,所以需要在Action中提供setter方法,讓spring將業(yè)務(wù)邏輯對象注入過來
3、在struts-config.xml文件中配置Action
??* <action>標(biāo)簽中的type屬性需要修改為org.springframework.web.struts.DelegatingActionProxy
?? DelegatingActionProxy是一個Action,主要作用是取得BeanFactory,然后根據(jù)<action>中的path屬性值
?? 到IoC容器中取得本次請求對應(yīng)的Action
??
4、在spring配置文件中需要定義struts的Action,如:
?<bean name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype">
??<property name="userManager" ref="userManager"/>
?</bean>
?* 必須使用name屬性,name屬性值必須和struts-config.xml文件中<action>標(biāo)簽的path屬性值一致
?* 必須注入業(yè)務(wù)邏輯對象
?* 建議將scope設(shè)置為prototype,這樣就避免了struts Action的線程安全問題
? ? ??
??
? ??