整合之前要搞清楚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. Ionic 中MD5加密使用

    1. 下载安装ts-md5 在项目的命令行工具里输入 npm  install ts-md5 --save 2. 使用 导入 import {Md5} from "ts-md5/dist/m ...

  2. Zigbee 的 mesh功能设置

    1. 在编译选项中加入ZIGBEEPRO 最大的节点深度为20,网络模式为mesh网络 如果不配置,则为HOME_CONTROLS,支持5级路由深度,每个路由器最多可连接20个节点(最多包括6个路由器 ...

  3. framework4.0 IIS7下urlrewriter设置问题

    framework4.0 IIS7下urlrewriter设置问题 http://www.cnblogs.com/litian/articles/alex.html IIS开启伪静态后html静态页面 ...

  4. yii framework config 可以被配置的项目

    http://hi.baidu.com/lossless1009/item/990fdb33a52ffcf1e7bb7a4c <?php002 003 // 取消下行的注释,来定义一个路径别名0 ...

  5. 新玩具,React v16.7.0-alpha Hooks

    周五看见React v16.7.0-alpha Hooks,今早起来看见圈里已经刷屏了Hooks,正好周末,正好IG和G2的比赛还没开始,研究下... 刚刚接触react时候非常喜欢用函数式组件,因为 ...

  6. 【Spring学习笔记-MVC-11--】Spring MVC之表单标签

    一.使用方法 1.要使用Spring MVC提供的表单标签,首先需要在视图页面添加: <%@ taglib prefix="form" uri="http://ww ...

  7. VMware NAT模式下设置网络

    一.虚拟机NAT模式原理 NAT模式在VMware下又称VMnet8.在这种模式下,宿主机有两块网卡,一块是真实的物理网卡(即NAT device),连接Network:一块是 VMware Netw ...

  8. Linux Shell脚本编程--Linux特殊符号大全

    Linux Shell脚本编程--Linux特殊符号大全 linux_shell 特殊符号的介绍 2011

  9. Linux期中架构 全网备份案例

    server端脚本 #!/bin/bash #1 进行数据完整性验证 并生成结果 find /backup -type f -name "finger.txt"| xargs md ...

  10. [UE4]IES光源概述文件

    IES Light Profiles(IES光源概述文件) 是一条曲线,该曲线在一段弧线中定义了光源强度,虚幻引擎4将会围绕某个轴旋转该弧线,从而使得 点光源 (和从技术上讲的 聚光源,下面会提供更多 ...