spring整合struts2

1. Spring和Struts2整合
(1)引入struts2和spring基本开发包
(2)在web.xml中添加spring容器创建配置
<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>
(3)引入struts2-spring-plugin.jar.启用将Action创建或寻找工作交给plugin插件的StrutsSpringObjectFactory
(4)struts.xml配置方式
a方案. 将Action对象交给Spring容器创建并注入
<action name="add" class="idOrNameOfAction">
</action>
idOrNameOfAction与spring容器中的Action组件名一致.
b方案. 将Action对象置于容器之外,有插件创建并完成容器对象注入
<action name="add" class="tarena.ssh2.action.AddDeptAction">
</action>
注意:Action类中的属性名要和Spring容器中对象的idOrName一致才能注入.因为默认采用byName方式注入.
如果需要按照byType将dao注入给action,可在struts.xml中添加以下配置
<constant name="struts.objectFactory.spring.autoWire" value="type"/>
----或者在struts.properties中添加----
struts.objectFactory.spring.autoWire=type
--------------------------------------
注意:ssh2整合时,需要jta.jar包.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/dataSource.xml,classpath:applicationContext.xml</param-value>
 </context-param>
 
  <filter>
      <filter-name>struts2Filter</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2Filter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 


posted on 2016-01-26 21:50  编世界  阅读(568)  评论(0编辑  收藏  举报