整合之前要搞清楚struts2是什么;

struts2:表现层框架  增删改查  作用域  页面跳转   异常处理  ajax 上传下载  excel   调用service

spring :IOC/DI:bean容器   aop

整合点:

1.struts2的所有对象 交由spring来管理  意味着struts.xml内对象生成方式要发生改变

2.初始化容器的时机  服务器启动时完成

    spring已有方案解决

整合步骤:

1.建web 工程 copy jar包

     2.copy     struts.xml

a:<constant name="struts.objectFactory" value="spring"/>这个是说明struts的对象交于spring来处理

         b:action元素 class 属性的值 不再写包名+类名  而是写spring配置文件中的bean的id值

3.copy web.xml

      监听器   启动时加载   spring的配置文件

      由于监听器默认 读WEB-INF 下面名字为 applicationContext.xml的配置文件,不能读其它文件

      所以需要再加一个配置:       

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

      意思是读取src下所有以applicationContext开头的文件

web.xml:    

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app id="WebApp_9" 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">
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <listener>
        <!-- 此监听器默认读WEB-INF 下的applicationContext.xml 文件 启动时加载我们的spring ioc 容器 -->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- context-param 元素由 ServletContext对象解析 意思是读取src下 以applicationContext开头的所有文件 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
      </context-param>
  </web-app>

action:

public class TestAction {
  public String execute(){
    System.out.println("============TestAction===========");
    return "success";
  }
}

applicationContext-action.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" >
  <bean id="testAction" class="com.huawei.s2s.action.TestAction" scope="prototype" >
  </bean>
</beans>

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.objectFactory" value="spring"/>
  <package name="default" namespace="/" extends="struts-default">
    <action name="testAction" class="testAction" ><--注意这里class的值-->
      <result name="success">/index.jsp</result>
    </action>
  </package>
</struts>

spring 整合 struts2 xml配置的更多相关文章

  1. spring 整合 hibernate xml配置

    spring 整合 hibernate: hibernate :对数据库交互 spring: ioc   aop 整合点: 1.sessionFactory对象不再由hibernate生成,交由spr ...

  2. Spring整合Struts2 XML版

    1.jar包 <!--spring配置--> <dependency> <groupId>org.springframework</groupId> & ...

  3. Spring整合Struts2的配置与测试

    整合目的 让Spring的IOC容器管理Struts2的Action 整合步骤 1.新建一个Web项目 2.加入Spring的jar包和添加Spring的配置文件 3.在Web.xml中配置Conte ...

  4. 一 SSH整合:Spring整合Struts2的两种方式,struts.xml管理Action&Bean管理Action

    SSH回顾 1 引入jar包 Struts2的jar包 D:\Struts2\struts-2.3.35\apps\struts2-blank\WEB-INF\lib  开发基本包 Struts2有一 ...

  5. Struts2的使用以及Spring整合Struts2

    一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...

  6. Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)

    1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...

  7. Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。

    1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...

  8. Spring框架学习(5)spring整合struts2

    内容源自:spring整合struts2 一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成i ...

  9. Spring整合Struts2的方法

    一.基本支持 通常我们整合Spring和struts2的目的是让Spring来管理struts2的控制器.也就是说把Action交由Spring来管理,利用IOC的特性把Action注入到业务逻辑中. ...

随机推荐

  1. C/C++基础----泛型算法

    算法不依赖与容器(使用迭代器),但大多数依赖于元素类型.如find需要==运算符,其他算法可能要求支持<运算符. 算法永远不会执行容器的操作,永远不会改变底层容器的大小(添加或删除元素). ac ...

  2. Javascript中的闭包(六)

      一.什么是闭包  函数可以记住并访问所在词法作用域时,就产生了闭包,即使在词法作用域外调用函数. (也就是说如果一个函数在执行完之后,其中的内部包含的函数仍然对该函数的作用域持有着引用(函数执行完 ...

  3. python使用smtplib发送邮件

    python要实现发送邮件的功能,需要使用smtplib库. 1. 过程大致如下: 1. 建立和SMTP邮件服务器的连接 # 默认端口25 smtp = smtplib.SMTP(host, port ...

  4. R语言学习——欧拉计划(1)Multiples of 3 and 5

    [题目一]If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. ...

  5. 开发框架-APP:Hybird App

    ylbtech-开发框架-APP:Hybird App Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间的app,兼具“Native App良好用户交互体 ...

  6. selenium Java-1 配置

      1.使用intellij新建一个maven项目,名字自定.在pom中写入selenium的依赖.其他依赖也添加到该文件中. [maven selenium依赖](http://mvnreposit ...

  7. Vmware 安装CentOS 6.5

    转自:http://www.centoscn.com/image-text/install/2014/1209/4281.html 其实通过VM安装虚拟机还是蛮简单的,只不过有个别选项可能导致大家安装 ...

  8. javascript节点操作insertBefor()

    如果想要把节点放在某个特定的位置,而不是放在末尾,就可以使用insertBefore(a,b) 参数a:要插入的节点 参数b:作为参照的节点. var oDiv = document.getEleme ...

  9. Hive基础之Hive表常用操作

    本案例使用的数据均来源于Oracle自带的emp和dept表 创建表 语法: CREATE [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name ...

  10. Hadoop(1.2.1)安装

    背景知识: 1.数据分布存储,不是复制存储 2.数据不动,代码动,由于分布式存储,所以把代码移动到数据的地方计算. 3.数据如何分割,hadoop提供的分割文件的编程接口 安装: 1.安装JDK 1. ...