http://blog.csdn.net/yerenyuan_pku/article/details/52894958

前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Struts1.3.8,但是集成的方案并不完美,因为我们在Action里面每次要取得Spring容器实例,都必须有这样的代码:

WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());

是不是很招人烦啊!而且这还会导致Struts的action跟Spring紧密耦合,因为我们使用到了Spring里面的类了。这时我们就想能不能采用Spring依赖注入的方式直接将其注入进来呢?我们应知道如果我们要使用Spring的依赖注入,前提是bean必须交给Spring容器进行管理。所以,我们要把action交给Spring管理,这样我们可以使用依赖注入在action中注入业务层的bean。注意,要确保action的path属性值与bean的名称相同。如果Struts配置文件中的<action>元素为:

<action path="/person/list" ...></action>

那么在把action交给Spring管理时,Spring配置文件中应添加如下配置:

<bean name="/person/list" class="cn.itcast.web.action.PersonAction"/>

除此之外,我们还要在Struts配置文件中添加进Spring的请求控制器,该请求控制器会先根据action的path属性值到Spring容器中寻找跟该属性值同名的bean,如果寻找到即使用该bean处理用户请求。即在Struts配置文件中添加如下配置:

<controller><set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" /> </controller>

至此,Struts配置文件——struts-config.xml的内容就是:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN""http://struts.apache.org/dtds/struts-config_1_3.dtd"><struts-config><action-mappings> <action path="/person/list" validate="false"> <forward name="list" path="/WEB-INF/page/personlist.jsp"></forward> </action> </action-mappings> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" /> </controller> </struts-config>

Spring配置文件——beans.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: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.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"> <context:annotation-config /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&amp;characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="yezi" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1" /> <!-- 连接池的最大值 --> <property name="maxActive" value="500" /> <!-- 最大空闲值。当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2" /> <!-- 最小空闲值。当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 数据源 --> <property name="mappingResources"> <list> <value>cn/itcast/bean/Person.hbm.xml</value> <!-- Hibernate的实体bean的映射文件(可有多个) --> </list> </property> <!-- hibernateProperties是用来配置Hibernate的属性信息 --> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false </value> </property> </bean> <!-- 配置针对Hibernate的事务管理器,事务管理器对sessionFactory对象创建出来的session进行管理 --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager" /> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" /> <bean name="/person/list" class="cn.itcast.web.action.PersonAction" /> </beans>

注意一点,我们要向SSH项目中导入如下jar文件: 
 
否则org.springframework.web.struts.DelegatingRequestProcessor该类将会找不到,org.springframework.web.struts.DelegatingRequestProcessor该类的处理过程为: 
 
这样,总共需要向SSH项目中导入的jar文件有47个: 
 
如此一来,PersonAction的代码就应该改为:

public class PersonAction extends Action { @Resource PersonService personService; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute("persons", personService.getPersons()); return mapping.findForward("list"); } }

我们采用这种方式,可以看到并没有使用到Spring里面的任何类。查看数据库person表,可以看到person表有如下记录: 
 
这时,我们通过浏览器访问url地址:http://localhost:8080/SSH/person/list.do,可以看到如下结果: 

至此,说明Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成的第二种方案成功了,而且这种方式更加优雅,实际开发中就该这样做。如须查看源码,可点击Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案二进行下载。

转载于:https://www.cnblogs.com/telwanggs/p/6913561.html

(转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案二相关推荐

  1. (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一

    http://blog.csdn.net/yerenyuan_pku/article/details/52888808 前面我们已经集成了Spring4.2.5+Hibernate4.3.11这两个框 ...

  2. (转)Spring4.2.5+Hibernate4.3.11+Struts2.3.24整合开发

    http://blog.csdn.net/yerenyuan_pku/article/details/52902851 前面我们已经学会了Spring4.2.5+Hibernate4.3.11+Str ...

  3. (转)Spring4.2.5+Hibernate4.3.11组合开发

    http://blog.csdn.net/yerenyuan_pku/article/details/52887573 搭建和配置Spring与Hibernate整合的环境 今天我们来学习Spring ...

  4. 飞桨常规赛:黄斑中央凹定位(GAMMA挑战赛任务二) - 11月第3名方案

    飞桨常规赛:黄斑中央凹定位(GAMMA挑战赛任务二) 11月第3名方案--鸣沙山下.伽利略 比赛地址:https://aistudio.baidu.com/aistudio/competition/d ...

  5. 飞桨常规赛:PALM眼底彩照中黄斑中央凹定位-11月第1名方案

    飞桨常规赛:PALM眼底彩照中黄斑中央凹定位-11月第1名方案 (1)比赛介绍 赛题介绍 榜首个人主页,戳此处查看 PALM黄斑定位常规赛的重点是研究和发展与患者眼底照片黄斑结构定位相关的算法.该常规 ...

  6. struts2.3.12+hibernate4.3.11+spring4.2.2整合问题2java.lang.ClassNotFoundException: org.springframework.w

    在spring jar 包下有spring-web-4.2.2.RELEASE.jar 包,但总是报错 java.lang.ClassNotFoundException:     org.spring ...

  7. 基于Spring4+SpringMVC4+Mybatis3+Hibernate4+Junit4框架构建高性能企业级的部标1077视频监控平台...

    开发企业级的部标GPS监控平台,投入的开发力量很大,开发周期也很长,选择主流的开发语言以及成熟的开源技术框架来构建基础平台,是最恰当不过的事情,在设计之初就避免掉了技术选型的风险,避免以后在开发过程中 ...

  8. OS10.11安装Cocoapods并集成ReactiveCocoa

    最近换了一台机器, 发现上面没有安装Cocoapods, 因为自己的机器是在10.10的时候安装的没有发现问题, 更新了10.11之后发现cocoapods安装真是一个浩大的工程(那是之前没找对方法) ...

  9. shiro学习(11):servelet实现权限认证二

    工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 在pom.xml里面添加 <dependency> ...

最新文章

  1. qt 5.9 mysql 5.7_Qt 5.9.1 连 MYSQL 5.7数据库
  2. Nginx + Node + Vue 部署初试(修改)
  3. Java 为 Excel 中的行设置交替背景色
  4. select unit_timestamp(); 和select unit_timestamp(1970-1-1 08:00:00)和 select from_unixtime(1)...
  5. Win7电脑快速获取超级管理员权限的方法
  6. 相机标定中标定棋盘的角点是哪个?
  7. csdn2020年度博客之星 - 直播间(恭喜圆满结束)
  8. 线性代数学习笔记(二)
  9. 面试总结之人工智能AI(Artificial Intelligence)/ 机器学习(Machine Learning)
  10. xenserver安装使用
  11. java单例模式深入详解_javascript 模块依赖管理的本质深入详解
  12. express中间件和路由教程
  13. 20200607每日一句
  14. linux查看系统信息命令 转自:http://blog.chinaitlab.com/html/31/1365331-180901.html
  15. Java SimpleDateFormat 中英文时间格式化转换
  16. python创建Excel表格
  17. 学习笔记 java学习(三)
  18. 达内python第一次月考题目_月考来临!第一次月考远比你想象的重要!
  19. u-boot使用bootargs给内核传参数
  20. 医学图像处理与深度学习入门

热门文章

  1. (84)FPGA面试题-多bit跨时钟域
  2. 不同性能极限下的服务器,探求极限性能 服务器测试之ScienceMark
  3. 12002.i2ctools工具
  4. 1014.修改clion的工具链
  5. nginx的模块化体系结构
  6. Mysql简介和Mysql优化查询的方法
  7. “手把手教你学linux驱动开发”OK6410系列之02---虚拟字符设备
  8. STM32H7---高速缓存Cache(一)
  9. html 透视效果,html – CSS – 对背景图像的“敲除”/透视效果
  10. 加载osgb数据转换不能用_在ArcGIS Pro中OSGB数据转换及发布服务流程