Spring与Struts框架整合

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:保存成功!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/539554.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

esxi能直通的显卡型号_显卡刷bios教程

一般来说显卡默认的出厂bios就已经很稳定&#xff0c;如果没有特殊情况下建议不要刷显卡bios。一般而言部分网友刷显卡BIOS目的是开核或超频&#xff0c;那么对于一个不会刷显卡bios的网友来说肯定会问显卡怎么刷bios类似的问题&#xff0c;那么本文这里就说一下有关显卡怎么刷…

关于Linux网卡调优之:RPS (Receive Packet Steering)

昨天在查LVS调度均衡性问题时&#xff0c;最终确定是 persistence_timeout 参数会使用IP哈希。目的是为了保证长连接&#xff0c;即一定时间内访问到的是同一台机器。而我们内部系统&#xff0c;由于出口IP相对单一&#xff0c;所以总会被哈希到相同的RealServer。 过去使用LVS…

footer.php置底,CSS五种方式实现Footer置底

页脚置底(Sticky footer)就是让网页的footer部分始终在浏览器窗口的底部。当网页内容足够长以至超出浏览器可视高度时&#xff0c;页脚会随着内容被推到网页底部&#xff1b;但如果网页内容不够长&#xff0c;置底的页脚就会保持在浏览器窗口底部。方法一&#xff1a;将内容部分…

安卓adapter适配器作用_自带安卓系统的便携屏,能玩出什么花样?

之前说到去年出差太多&#xff0c;平常就把便携屏带上了。之前也说了如果是像笔者这样的出差狗也知道&#xff0c;托运需要提前去机场一路着急忙慌&#xff0c;不托运只需要打印登机牌(纸质才给报销)排队安检登机就完了。有的时候可以把标准显示器来回寄&#xff0c;只要包装强…

Gradle插件学习笔记(二)

之前介绍了Gradle插件的开发&#xff0c;这次会对功能进行一部分拓展&#xff0c;建议没有读过第一篇文章的朋友&#xff0c;先看一下Gradle插件学习笔记&#xff08;一&#xff09; Extension 之前的文章提到过&#xff0c;如何编写一个插件&#xff0c;但是并不能通过外面传递…

php抽象类继承抽象类,PHP面向对象程序设计高级特性详解(接口,继承,抽象类,析构,克隆等)...

本文实例讲述了PHP面向对象程序设计高级特性。分享给大家供大家参考&#xff0c;具体如下&#xff1a;静态属性class StaticExample {static public $aNum 0; // 静态共有属性static public function sayHello() { // 静态共有方法print "hello";}}print StaticExam…

Typora markdown公式换行等号对齐_Typora编写博客格式化文档的最佳软件

Typora-编写博客格式化文档的最佳软件Typora 不仅是一款支持实时预览的 Markdown 文本编辑器&#xff0c;而且还支持数学公式、代码块、思维导图等功能。它有 OS X、Windows、Linux 三个平台的版本&#xff0c;是完全免费的。作为技术人员或者专业人员&#xff0c;使用Markdown…

Bootstrap静态cdn

百度的静态资源库的 CDN 服务http://cdn.code.baidu.com/ &#xff0c;访问速度更快、加速效果更明显、没有速度和带宽限制、永久免费,引入代码如下&#xff1a; <!-- 新 Bootstrap 核心 CSS 文件 --> <link href"http://apps.bdimg.com/libs/bootstrap/3.3.0/…

php复习,PHP排序算法的复习和总结

直接上代码吧&#xff01;/** 插入排序(一维数组)* 每次将一个待排序的数据元素&#xff0c;插入到前面已经排好序的数列中的适当的位置&#xff0c;使数列依然有序&#xff1b;直到待排序的数据元素全部插入完成为止。*/function insertSort($arr){if(!is_array($arr) || coun…

docker-machine

vbox安装 sudo /sbin/vboxconfig &#xfffc; yum install gcc make yum install kernel-devel-3.10.0-514.26.2.el7.x86_64 转载于:https://www.cnblogs.com/yixiaoyi/p/dockermachine.html

intention lock_写作技巧:你写出来的情节有用吗?好情节的原则——LOCK系统

读者喜欢一本小说的原因只有一个&#xff1a;很棒的故事。——Donald Maass来&#xff0c;话筒对准这位小作家&#xff0c;请问你是如何构思故事的&#xff1f;是习惯于现在脑海中把故事都想好了&#xff0c;才开始写作&#xff1f;还是习惯于临场发挥&#xff0c;喜欢一屁股坐…

zookeeper基本操作

1.客户端连接 [txtest1 bin]$ jps 23433 Jps 23370 QuorumPeerMain #zookeeper进程[txtest1 bin]$ ./zkCli.sh -server test1:2182 Connecting to test1:2182 2018-01-24 23:42:09,024 [myid:] - INFO [main:Environment100] - Client environment:zookeeper.version3.4.5-…

sqllite java 密码,SQLite登录检查用户名和密码

我正在创建一个应用程序(使用Java和SQLite)(JFrame&#xff0c;使用Netbeans)我有我想要登录的用户 . (我有所有正确的包JDBC&#xff0c;SQLite等)我遇到的问题似乎是获取用户名/密码来检查我的users.db文件..我正在使用Java和SQLite . 我也在使用JDBC .我的一些代码作为一个例…

springmvc与struts2的区别

1&#xff09;springmvc的入口是一个servlet&#xff0c;即前端控制器&#xff0c;例如&#xff1a;*.action struts2入口是一个filter过虑器&#xff0c;即前端过滤器&#xff0c;例如&#xff1a;/* 2&#xff09;springmvc是基于方法开发&#xff0c;传递参数是通过方法形…

power designer数据流图_鲲云公开课 | 三分钟带你了解数据流架构

目前&#xff0c;市场上的芯片主要包括指令集架构和数据流架构两种实现方式。指令集架构主要包括X86架构、ARM架构、精简指令集运算RISC-V开源架构&#xff0c;以及SIMD架构。总体来说&#xff0c;四者都属于传统的通用指令集架构。传统的指令集架构采用冯诺依曼计算方式&#…

onCreate源码分析

原文地址Android面试题-onCreate源码都没看过&#xff0c;怎好意思说自己做android Activity扮演了一个界面展示的角色&#xff0c;堪称四大组件之首&#xff0c;onCreate是Activity的执行入口&#xff0c;都不知道入口到底干了嘛&#xff0c;还学什么android,所以本文会从源码…

linux php环境搭建教程,linux php环境搭建教程

linux php环境搭建的方法&#xff1a;首先获取相关安装包&#xff1b;然后安装Apache以及mysql&#xff1b;接着修改配置文件“httpd.conf”&#xff1b;最后设置环境变量和开机自启&#xff0c;并编译安装PHP即可。一、获取安装包PHP下载地址&#xff1a;http://cn.php.net/di…

Apache2.2与Tomcat7集成方案详解

原文地址&#xff1a;http://my.oschina.net/u/919173/blog/159206 ------------------------------------ 首先谈一下为什么要集成Apache和tomcat7&#xff1f; Apache是当前使用最为广泛的WWW服务器软件&#xff0c;具有相当强大的静态HTML处理的能力。 Tomcat服务器是一个…

cocos 制作动态生成内容的列表_零代码工具,让你在线轻松制作交互内容!

在工作中设计师不会写代码&#xff0c;懂代码的不会设计&#xff0c;2种不同工作互掐的情况屡见不鲜&#xff0c;那我们如何把这2项工作一并融合贯通呢&#xff1f;对于不懂代码的职场“小白”&#xff0c;我们可以利用一些零代码工具来完成。今天小编介绍几款在线开发设计工具…

php怎样数据缓存文件,php数据缓存到文件类设计

// 自定义缓存类class Cache_Filesystem {// 缓存写保存function set ($key, $data, $ttl) {//打开文件为读/写模式$h fopen($this->get_filename($key), ‘a‘);if (!$h) throw new Exception("Could not write to cache");flock($h, LOCK_EX); //写锁定&#x…