javaEE SSH三大框架整合,Spring整合Struts2

导包:https://pan.baidu.com/s/1I55r1eKRmRd9lDcoT2gWBw   密码:ve1z


src/applicationContext.xml(Spring配置文件,配置Struts2中的Action对象):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns="http://www.springframework.org/schema/beans" 
		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/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
							http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
							http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
							http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

	<!-- 导入beans命名空间(约束) xmlns="http://www.springframework.org/schema/beans"
		导入context命名空间(约束) (读取properties文件) xmlns:context="http://www.springframework.org/schema/context"
		导入aop命名空间(约束) (通知织入目标对象) xmlns:aop="http://www.springframework.org/schema/aop"
		导入tx命名空间(约束) (事务) xmlns:tx="http://www.springframework.org/schema/tx"
	 -->
	
	<!-- Action配置(Spring整合Struts2) -->
	<!-- 注意:Action对象作用范围一定是多例的(每个请求(线程)都对应一个Action对象;servlet是多线程的).这样才符合struts2架构 -->
	<bean name="userAction" class="cn.xxx.web.action.UserAction" scope="prototype" >
		<!-- 手动配置注入Action的依赖属性 -->
		<property name="userService" ref="userService" ></property>
	</bean>
	<!-- service -->
	<bean name="userService" class="cn.xxx.service.impl.UserServiceImpl" >
		<property name="ud" ref="userDao" ></property>
	</bean>
	<!-- dao -->
	<bean name="userDao" class="cn.xxx.dao.impl.UserDaoImpl" >
		<!-- 注入sessionFactory -->
		<property name="sessionFactory" ref="sessionFactory" ></property>
	</bean>
	
</beans>

src/struts.xml(Struts2配置,配置objectFactory常量,配置Action的class属性):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

	<!-- 配置常量
		struts.objectFactory = spring	将action的创建权交给spring容器管理	
		struts.objectFactory.spring.autoWire = name  spring负责注入Action依赖属性(default.properties配置文件中已经默认配置了,不需要再配置了) -->
	<constant name="struts.objectFactory" value="spring"></constant>

	<package name="crm" namespace="/" extends="struts-default" >
	
		<!-- 整合方案1(不推荐):class属性上仍然配置action的完整类名 (不推荐理由:Action的生命周期最好由Spring管理,Spring中的aop等功能不能应用到Action对象中)
				效果:Action的创建由Struts2负责,Action的依赖属性的注入由spring自动完成(自动注入前提:spring中的bean的名字必须要与依赖属性名相同)
		 -->
		<!-- 整合方案2(推荐):class属性上填写spring中action对象的BeanName
				效果:Action的创建(生命周期)由spring负责,(注意)Action的依赖属性需要在Spring中手动配置注入
		 -->
		<action name="UserAction_*" class="userAction" method="{1}" >  <!-- class填写Spring配置中的BeanName -->
			<result name="toHome" type="redirect" >/index.htm</result>
			<result name="error" >/login.jsp</result>
		</action>
	</package>
</struts>
	

WEB-INF/web.xml(web项目配置,配置Spring监听器,配置Struts2的核心过滤器):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<display-name>MyWeb</display-name>
	
	<!-- 通过监听器Listener,可以让spring容器随web项目的启动而创建,随项目的关闭而销毁.(可以保证Spring容器是单例的) -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 配置spring配置文件位置参数 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!-- 扩大Hibernate中与数据库的连接会话session的作用范围
		注意: 任何filter一定要在struts的filter之前调用
	 -->
	 <filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	</filter>
	
	<!-- struts2核心过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<welcome-file-list>
	  <welcome-file>index.html</welcome-file>
	  <welcome-file>index.htm</welcome-file>
	  <welcome-file>index.jsp</welcome-file>
	  <welcome-file>default.html</welcome-file>
	  <welcome-file>default.htm</welcome-file>
	  <welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82108653