一、spring 与 struts整合的关键点
Spring,负责对象对象创建
Struts, 用Action处理请求
Spring与Struts框架整合,
关键点:action对象交给spring创建。
配置spring的监听器(ContextLoaderListener)
加载配置文件,初始化IOC容器。
配置struts的核心过滤器,拦截请求资源。
二、整合步骤
1)引入jar包
struts相关jar文件
spring-core 相关jar文件
spring-web 相关jar文件
spring-web-3.2.5.RELEASE.jar 【Spring源码】
struts2-spring-plugin-2.3.4.1.jar 【Struts源码】
2)配置
struts.xml
【struts路径与action映射配置】
bean-dao.xml
bean-serice.xml
【spring ioc容器配置】
web.xml
【核心过滤器: 引入struts功能】
【初始化spring的ioc容器】
2.1 导包
2.2 配置XML
struts.xml 【请求路径与action映射配置】
bean.xml 【spring ioc容器配置】
web.xml
【配置核心过滤器: 引入struts功能】
【初始化spring的ioc容器】
2.2.1 web.xml
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>
/WEB-INF/classes/bean-dao.xml,
/WEB-INF/classes/bean-service.xml,
/WEB-INF/classes/bean-action.xml
param-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
>>>>>> 配置struts核心过滤器
struts核心过滤器的作用处理拦截请求资源,
将原本的servlet流程转入struts流程。
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
>>>>>> 配置spring监听器
spring监听器的作用是加载多个配置文件,初始化IOC容器
+++ 配置加载多个配置文件:
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>
/WEB-INF/classes/bean-dao.xml,
/WEB-INF/classes/bean-service.xml,
/WEB-INF/classes/bean-action.xml
param-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
+++ 配置加载N个配置文件:
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>/WEB-INF/classes/bean*.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
2.2.2 bean*.xml
配置bean节点,加入IOC容器
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="helloDao" class="org.jsoft.hello.HelloDao">bean>
beans>
1.2.3 struts.xml
配置请求路径与资源的映射。
原本class指向完整类名,现在指向spring容器中的bean的ID名称。
<struts>
<package name="xxxx" extends="struts-default">
<action name="hello" class="helloAction" method="save">
<result name="success">/index.jspresult>
action>
package>
struts>
三、spring-struts 案例
2.1 配置XML
>>>>>> web.xml
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>
/WEB-INF/classes/bean-dao.xml,
/WEB-INF/classes/bean-service.xml,
/WEB-INF/classes/bean-action.xml
param-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
>>>>>> bean-dao.xml
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="helloDao" class="org.jsoft.hello.HelloDao">bean>
beans>
>>>>>> bean-service.xml
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="helloService" class="org.jsoft.hello.HelloService">
<property name="helloDao" ref="helloDao">property>
bean>
beans>
>>>>>> bean-action.xml
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="helloAction" class="org.jsoft.hello.HelloController" >
<property name="helloService" ref="helloService">property>
bean>
beans>
>>>>>> struts.xml
<struts>
<package name="xxxx" extends="struts-default">
<action name="hello" class="helloAction" method="save">
<result name="success">/index.jspresult>
action>
package>
struts>
2.2 配置实体
>>>>>> HelloDao.java
public class HelloDao {
public void save() {
System.out.println("DB 保存用户成功");
}
}
>>>>>> HelloService.java
public class HelloService {
private HelloDao helloDao;
public void setHelloDao(HelloDao helloDao) {
this.helloDao = helloDao;
}
public void save() {
helloDao.save();
}
}
>>>>>> HelloController.java
public class HelloController extends ActionSupport{
private HelloService helloService;
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
public String save() {
helloService.save();
return SUCCESS;
}
}
>>>>>> index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
spring-struts结合
body>
html>
2.3 测试
将服务部署到服务器上,浏览器访问路径
http://localhost:8080/my_spring_struts/hello,
会自动跳转到index.jsp资源。