Spring,负责对象对象创建
Struts,用Action处理请求
Spring与Struts框架整合,关键点:让struts框架action对象的创建,交给spring完成!

1.步骤:

引入jar文件
1)引入struts .jar相关文件

commons-fileupload-1.2.2.jar
       commons-io-2.0.1.jar
       commons-lang3-3.1.jar
       freemarker-2.3.19.jar
       javassist-3.11.0.GA.jar
       ognl-3.0.5.jar
       struts2-core-2.3.4.1.jar
       xwork-core-2.3.4.1.jar

2)spring-core 相关jar文件

commons-logging-1.1.3.jar
      spring-beans-3.2.5.RELEASE.jar
      spring-context-3.2.5.RELEASE.jar
      spring-core-3.2.5.RELEASE.jar
      spring-expression-3.2.5.RELEASE.jar

3)spring-web 支持jar包
      spring-web-3.2.5.RELEASE.jar 【Spring源码】
      struts2-spring-plugin-2.3.4.1.jar 【Struts源码】

4)配置XML
struts.xml

----struts路径与action映射配置
bean.xml

----spring ioc容器配置:先dao,再service,最后action
web.xml
    -----核心过滤器: 引入struts功能
    -----初始化spring的ioc容器

2.具体的代码

HelloDao.java

package com.zengmg.dao;public class HelloDao {public HelloDao(){System.out.println("Dao构造函数");}public void save(){System.out.println("Dao:保存成功!");}}

HelloService.java

package com.zengmg.service;import com.zengmg.dao.HelloDao;public class HelloService {public HelloService(){System.out.println("service构造函数");}//springIOC容器注入private HelloDao hdao;public void setHdao(HelloDao hdao) {this.hdao = hdao;}public void saveHello(){System.out.println("service:保存");hdao.save();}}

HelloAction.java

package com.zengmg.action;import com.opensymphony.xwork2.ActionSupport;
import com.zengmg.service.HelloService;public class HelloAction extends ActionSupport{private static final long serialVersionUID = 1L;//springIOC容器注入private HelloService hService;public void sethService(HelloService hService) {this.hService = hService;}@Overridepublic String execute() throws Exception {System.out.println("访问了helloAction");hService.saveHello();return SUCCESS;}
}

3、具体配置文件

bean-dao.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"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><bean id="helloDao" class="com.zengmg.dao.HelloDao" lazy-init="false"/></beans>     

bean-service.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"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><bean id="helloService" class="com.zengmg.service.HelloService"><property name="hdao" ref="helloDao"/></bean></beans>     

bean-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"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 指定action多例 --><bean id="helloAction" class="com.zengmg.action.HelloAction" scope="prototype"><property name="hService" ref="helloService"/></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><!--package 定义一个包,包作用:管理action,通常一个业务模块用一个包name包名不能重复 extends 当前包继承自哪个包abstract 表示当前包是否为抽象包,抽象包不能有action的定义,否则运行时报错。abstract=true:只有当前包被其他包继承时才使用。namespace 默认"/",是访问路径的一部分。action 配置请求路径与Action类的映射关系name 请求路径名称class 请求处理的action类的全名method 请求处理方法resultname action处理方式返回值type 跳转的结果类型标签体中指定跳转的页面--><package name="xxxx" extends="struts-default"><!-- <action name="hello" class="com.zengmg.action.HelloAction" method="execute"><result name="success">/success.jsp</result></action> --><!-- 此处的action不能用struts的,要用spring生成的id=helloAction的bean --><action name="hello" class="helloAction" method="execute"><result name="success">/success.jsp</result></action></package></struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1"><!-- 其他拦截器,其他拦截器要放在struts上面,要不然无效,因为struts拦截了所有请求 --><filter><filter-name>struts</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- spring配置 --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/bean-*.xml</param-value></context-param><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>

4、运行效果

在启动时,就提示:

Dao构造函数
service构造函数

浏览器访问:http://localhost:8080/hellostruts2/hello

访问了helloAction
service:保存
Dao:保存成功!

Spring与Struts框架整合相关推荐

  1. ssh(Struts+spring+Hibernate)三大框架整合-简述

    ssh(Struts+spring+Hibernate)三大框架配合使用来开发项目,是目前javaee最流行的开发方式,必须掌握: 注意: 为了稳健起见,每加入一个框架,我们就需要测试一下,必须通过才 ...

  2. 5.6 Spring与Struts 2整合应用

    2019独角兽企业重金招聘Python工程师标准>>>  开发一个Spring与Struts 2的整合项目的步骤如下. 1 创建Web项目Struts_Spring 2 添加Str ...

  3. (十)Spring 与 MVC 框架整合

    Spring 整合 MVC 目录 MVC 框架整合思想 为什么要整合 MVC 框架 搭建 Web 运行环境 Spring 整合 MVC 框架的核心思路 1. 准备工厂 2. 代码整合 Spring 整 ...

  4. Spring 与 MVC 框架整合思路

    Spring 整合 MVC MVC 框架整合思想 为什么要整合 MVC 框架 搭建 Web 运行环境 Spring 整合 MVC 框架的核心思路 1. 准备工厂 2. 代码整合 Spring 整合 S ...

  5. Spring系列(七)、Spring与MyBatis框架整合

    7 搭建Spring与MyBatis的集成环境 要实现Spring与MyBatis的整合,很明显需要这两个框架各自的jar包,以及整合两个框架的中间包mybatis-spring.jar: 我们使用m ...

  6. 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓

    hibernate工作原理 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久化操作 6.提 ...

  7. Spring+SpringMVC+Mybatis框架整合流程

    一:基本步骤 新建Maven项目,导入相关依赖.(推荐) ----–Mybatis配置 ------ 新建entity包,并根据数据库(表)新建相关实体类. 新建dao包,并根据业务创建必要的mapp ...

  8. 最详细的Spring+SpringMVC+Mybatis框架整合及mybatis分页讲解,适合初级者

    最详细的关于idea整合ssm框架讲解 一个关于brand(品牌)的项目 [ssm框架搭建源代码及mysql数据库数据]链接:https://pan.baidu.com/s/1eBogklK0rFLj ...

  9. SSM(Mybatis + Spring + Spring MVC)框架整合详细步骤(附jar包和项目下载,免费的)

    本例从0开始逐一整合SSM的步骤,实现基础的CRUD 开发环境:Eclipse4.6 + jdk1.8 + Tomcat8 + MySQL SSM所需jar包和项目下载路径在文章最后 1.数据库 创建 ...

最新文章

  1. linux动态分配全局置换,深入理解计算机系统 第九章 虚拟存储器
  2. Java后台管理系统,开箱即用
  3. Mining Precision Interface From Query Logs -- 学习笔记(一)
  4. 如何使用命令行拿到SAP Kyma的Lambda Function明细
  5. 【HDU - 5963】朋友(博弈,思维,必胜态必败态,找规律)
  6. 经典面试题SALES TAXES思路分析和源码分享
  7. 陕西居民医保微信缴纳教程来了,太方便了
  8. Python爬虫基础:验证码的爬取和识别详解
  9. EMR 配置纪录(不断更新)
  10. 知识竞赛时,倒计时字体的大小和位置如何调整?
  11. 聊一聊Android的第三方开发组件
  12. 台式计算机有线无线网卡设置,笔记本/台式电脑有线网络转无线wifi教程
  13. 如何将多个Excel 表合并成一个
  14. 自己使用的文本编辑器全部采用Tahoma字体
  15. 全国31个省份农产品进口出口额省级数据2001-2021
  16. 4-ipv6基础知识之-邻居发现协议NDP
  17. 运维开发面试题集锦(25k-35k)
  18. iOS开发- ios学习资源(持续更新)
  19. 我已经租不起回龙观的房子了
  20. git 命令使用(持续更新)

热门文章

  1. oracle03206,ORACLE数据库创建表空间ORA-03206报错的解决方案
  2. 万丰科技机器人排名_机器人系统集成“7宗最”
  3. 《团队激励与沟通》第 3 讲——沟通概述与沟通过程 重点部分总结
  4. java书面_Java程序猿的书面采访String3
  5. idea连接跳板机_跳板机服务(jumpserver)
  6. 光影mod_MOD墨设设计丨天海之间的乌托之城
  7. mysql 乘法_测试面试题集Python花式打印九九乘法口诀表
  8. kettle同步数据到hive 巨慢_超详细教程,kettle ETL mysql到hadoop hive数据抽取,值得收藏...
  9. mysql 导入 mssql_MySQL(csv,text)导入mssql使用方法
  10. logstash-input-redis插件使用详解