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;}
}
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>
在啟動時,就提示:
Dao構造函數
service構造函數
瀏覽器訪問:http://localhost:8080/hellostruts2/hello
訪問了helloAction
service:保存
Dao:保存成功!