八、框架整合(一) - Spring 与 Struts整合

一、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
<?xml version="1.0" encoding="UTF-8"?>
<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">

	<!-- 1.struts配置 :引入struts核心过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 2.spring配置 :加载xml,初始化ioc容器-->
	<context-param>
			<param-name>contextConfigLocation</param-name>
				<!-- <param-value>/WEB-INF/classes/bean*.xml</param-value> -->
			
		
				<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.ContextLoaderListener</listener-class>
	</listener>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

>>>>>> 配置struts核心过滤器
	struts核心过滤器的作用处理拦截请求资源,
	将原本的servlet流程转入struts流程。
	<!-- 1.struts配置 :引入struts核心过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
>>>>>> 配置spring监听器
		spring监听器的作用是加载多个配置文件,初始化IOC容器
	
	+++ 配置加载多个配置文件:
				<!-- 2.spring配置 :加载xml,初始化ioc容器-->
				<context-param>
						<param-name>contextConfigLocation</param-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.ContextLoaderListener</listener-class>
				</listener>
	
	 
	 +++ 配置加载N个配置文件:
				<!-- 2.spring配置 :加载xml,初始化ioc容器-->
				<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>	 

2.2.2 bean*.xml
		配置bean节点,加入IOC容器
<?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"
    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名称。
<?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 name="xxxx" extends="struts-default">
    	
    	
    	<action name="hello" class="helloAction" method="save">
    		<result name="success">/index.jsp</result>
    	</action>
       
    </package> 
</struts>

三、spring-struts 案例

2.1 配置XML
>>>>>> web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">

	<!-- 1.struts配置 :引入struts核心过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 2.spring配置 :加载xml,初始化ioc容器-->
	<context-param>
			<param-name>contextConfigLocation</param-name>
				<!-- <param-value>/WEB-INF/classes/bean*.xml</param-value> -->
			
		
				<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.ContextLoaderListener</listener-class>
	</listener>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

>>>>>> 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"
    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
<?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"
    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
<?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"
    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
<?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 name="xxxx" extends="struts-default">
    	
    	
    	<action name="hello" class="helloAction" method="save">
    		<result name="success">/index.jsp</result>
    	</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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	
	spring-struts结合
	
</body>
</html>
2.3 测试
  将服务部署到服务器上,浏览器访问路径
		  http://localhost:8080/my_spring_struts/hello,
	会自动跳转到index.jsp资源。
		  

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值