歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring和Struts整合

Spring和Struts整合

日期:2017/3/1 10:18:15   编辑:Linux編程

1.添加struts2包

2.在web.xml配置struts2過濾器

  1. <filter>
  2. <filter-name>struts2</filter-name>
  3. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>struts2</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>

3.編寫struts.xml,寫測試應用程序確保驗證struts配置正確

  1. <package name="h" extends="struts-default">
  2. <action name="login" class="org.ymm.actions.LoginAction" method="mlogin">
  3. <result name="success" type="redirect">login.jsp</result>
  4. <result name="fail">fail.jsp</result>
  5. </action>
  6. </package>

4.如果struts已經ok,添加spring支持包,配置監聽器ContextLoaderListener (讓spring先於struts被加載,為後來struts的action被spring管理)

注:必須要加struts2-spring-plugin-2.3.3.jar此包

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath:beans.xml</param-value>
  4. </context-param>
  5. <listener>
  6. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  7. </listener>
web.xml 的加載順序是:context-param -> listener -> filter -> servlet (由於listener先於filter,所以用listener啟動spring)

在struts2配置文件,加入 <constant name="struts.objectFactory" value="spring"></constant> 屬性

5.編寫beans.xml的bean,例如管理sturts中action的bean:

  1. <bean id="spring" class="org.ymm.actions.LoginAction">
  2. .......
  3. </bean>
Copyright © Linux教程網 All Rights Reserved