Spring和Struts整合


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.   
  6. <filter-mapping>  
  7.     <filter-name>struts2</filter-name>  
  8.     <url-pattern>/*</url-pattern>  
  9. </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.       
  6.  <listener>  
  7.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  8.  </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>  

相关内容