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