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.pswp.cn/news/539554.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/539554.shtml
英文地址,請注明出處:http://en.pswp.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…