spring整合struts---個人筆記

spring+struts的集成(第一種集成方案)
原理:在Action中取得BeanFactory對象,然後通過BeanFactory獲取業務邏輯對象

1、spring和struts依賴庫配置
 * 配置struts
  --拷貝struts類庫和jstl類庫
  --修改web.xml文件來配置ActionServlet
  --提供struts-config.xml文件
  --提供國際化資源文件
 * 配置spring
  --拷貝spring類庫
  --提供spring配置文件
  
2、在struts的Action中調用如下代碼取得BeanFactory
 BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());

 

or     BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-beans.xml");

3、通過BeanFactory取得業務對象,調用業務邏輯方法   
  
     
spring+struts的集成(第二種集成方案)
原理:將業務邏輯對象通過spring注入到Action中,從而避免了在Action類中的直接代碼查詢

1、spring和struts依賴庫配置
 * 配置struts
  --拷貝struts類庫和jstl類庫
  --修改web.xml文件來配置ActionServlet
  --提供struts-config.xml文件
  --提供國際化資源文件
 * 配置spring
  --拷貝spring類庫
  --提供spring配置文件
2、因爲Action需要調用業務邏輯方法,所以需要在Action中提供setter方法,讓spring將業務邏輯對象注入過來

3、在struts-config.xml文件中配置Action
  * <action>標籤中的type屬性需要修改爲org.springframework.web.struts.DelegatingActionProxy
   DelegatingActionProxy是一個Action,主要作用是取得BeanFactory,然後根據<action>中的path屬性值
   到IoC容器中取得本次請求對應的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>標籤的path屬性值一致
 * 必須注入業務邏輯對象
 * 建議將scope設置爲prototype,這樣就避免了struts Action的線程安全問題

 

 

web.xml加入:

 

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:applicationContext-*.xml</param-value>
 </context-param>

 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

 

 

struts-config.xml加入:

 

<action-mappings>

  <action path="/logininput" forward="/login.jsp"></action>

  <action path="/login"
   type="org.springframework.web.struts.DelegatingActionProxy"
   name="loginForm" scope="request">
   <forward name="success" path="/success.jsp" />
  </action>
 </action-mappings>
      
 

applicationContext.xml加入:

 

<bean id="userService" class="com.jxdb.service.UserServiceImpl"/>

 

<bean name="/login" class="com.jxdb.struts.action.LoginAction" scope="prototype">
  <property name="userService" ref="userService"/>
</bean>

 

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