當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring与Struts2整合的两种解决方案
生活随笔
收集整理的這篇文章主要介紹了
Spring与Struts2整合的两种解决方案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://www.itzhai.com/spring-and-struts2-integration-of-the-two-solutions.html
Struts2與Spring整合的方案一: (1)將struts2-spring-plugin-x-x-x.jar復制到工程的WEB-INF/lib目錄下,在該插件包中有個struts-plugin.xml文件,該文件的默認配置如下: <struts><bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" /><!-- Make the Spring object factory the automatic default --><constant name="struts.objectFactory" value="spring" /><package name="spring-default"><interceptors><interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/><interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/></interceptors></package> </struts> (2)在web.xml中配置Spring的監聽器: <context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-*.xml</param-value> </context-param> <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> (3)修改Struts.xml配置文件: 在配置Action時,需要將class屬性和Spring配置文件中的相對應的Action的bean的Id的屬性保持一致,系統即可通過Spring來裝配和管理Action。
如果action包含了Service層的對象:
則需要添加set方法,才可以使用Spring依賴注入:
public void setStudentInfoService(StudentInfoServiceImpl studentInfoService) {this.studentInfoService = studentInfoService; } 如果action在struts.xml如下配置: <action name="studentRegister" class="studentRegisterAction"><result name="result">/WEB-INF/exam/result.jsp</result><result name="input">/WEB-INF/exam/error.jsp</result><interceptor-ref name="excludeParamsStack" /> </action> 則在applicationContext.xml文件中該Action的配置如下: <bean id="studentRegisterAction" class="com.exam.actions.StudentRegisterAction" scope="prototype"><property name="studentInfoService"><ref bean="studentInfoService" /></property> </bean> Struts2與Spring整合的方案二: 前面兩個步驟和方案一的一樣;
在配置struts.xml文件時,Action的class為該Action的類路徑,而在applicationContext.xml配置文件中不需要添加Action的bean配置。這樣,當我們使用Action類時,由于studentInfoService已經配置了相關的bean,所以會自動裝配。
總結
以上是生活随笔為你收集整理的Spring与Struts2整合的两种解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Struts2工作原理和框架扩展点
- 下一篇: git rebase/reset小计