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集成方案二进行下载。

(转)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. spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明

    一.准备工作 开始之前,先参考上一篇: struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明 struts2.3 ...

  5. struts2.3.24 + spring4.1.6 + hibernate4.3.11+ mysql5.5.25开发环境搭建及相关说明

    一.目标 1.搭建传统的ssh开发环境,并成功运行(插入.查询) 2.了解c3p0连接池相关配置 3.了解验证hibernate的二级缓存,并验证 4.了解spring事物配置,并验证 5.了解spr ...

  6. Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)

    1. 在IntelliJ中新建maven项目 给出一个建好的示例 2. 在pom.xml中配置依赖 包括: spring-context spring-orm hibernate-core mysql ...

  7. Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)(Junit测试类)

    1. 在IntelliJ中新建maven项目 给出一个建好的示例 2. 在pom.xml中配置依赖 包括: spring-context spring-orm hibernate-core mysql ...

  8. spring4+springmvc+springdataJPA+hibernate4+Junit4整合懒加载问题

    文章目录 技术交流 #摘要 本文主要是为了解决"spring4+springmvc+springdataJPA+hibernate4+junit4整合",注解了OneToMany. ...

  9. MS CRM 2011的自定义和开发(11)——插件(plugin)开发(二)

    http://www.cnblogs.com/StoneGarden/archive/2012/02/06/2339490.html MS CRM 2011的自定义和开发(11)——插件(plugin ...

随机推荐

  1. HihoCoder1706 : 末尾有最多0的乘积(还不错的DP)

    描述 给定N个正整数A1, A2, ... AN. 小Hi希望你能从中选出M个整数,使得它们的乘积末尾有最多的0. 输入 第一行包含两个个整数N和M. 第二行包含N个整数A1, A2, ... AN. ...

  2. 通过HttpservletRequest对象获取客户端的真实IP地址

    这篇文章主要介绍了Java中使用HttpRequest获取用户真实IP地址,使用本文方法可以避免Apache.Squid.nginx等反向代理软件导致的非真实IP地址,需要的朋友可以参考下 在JSP里 ...

  3. [POI 2014] Little Bird

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3831 [算法] 单调队列优化动态规划 时间复杂度 : O(N) [代码] #incl ...

  4. JAVA 中 synchronized 详解

    看到一篇关于JAVA中synchronized的用法的详解,觉得不错遂转载之..... 原文地址: http://www.cnblogs.com/GnagWang/archive/2011/02/27 ...

  5. UI:地图和定位

    参考学习链接 各种IOS设备可以使用 Core Location 框架确定它的物理位置.core location 主要使用三种技术来实现功能.GPS.蜂窝基站三角网络定位. wifi 定位服务.这三 ...

  6. HDU5589:Tree(莫队+01字典树)

    传送门 题意 略 分析 f[u]表示u到根的边的异或 树上两点之间的异或值为f[u]^f[v], 然后将查询用莫队算法分块,每个点插入到字典树中,利用字典树维护两点异或值大于等于M复杂度O(N^(3/ ...

  7. HDU6031:Innumerable Ancestors(二分+倍增数组)

    传送门 题意 n个点的图,有n-1条无向边,m个询问,每次询问 给出两个集合a和b,找到a的一个元素x,b的一个元素y,使得x和y的lca深度最大 分析 这道题如果直接暴力做,复杂度为O(mk1k2* ...

  8. Codeforces277A 【dfs联通块】

    题意: 给出n个人会的语言类型,然后问这n个人里面还需要几个人学习一下语言就可以n个直接互通了.a会1,2,b会2,3,c会4,那么只要C学一下1或者2,或者3就好了...大致就是这个意思. 思路: ...

  9. hdoj1728【搜索的两种写法】

    以前的一道题目,现在拿到总觉得是DFS,然后T掉就没什么想法了,很狗的看了以前的写法(以前还是看题解的AC的),是BFS,每次都要转弯,但是之前你的达到一种他走到了死路,所以才是不得不转弯,写法也是非 ...

  10. 51单片机 小车 L298N pwm调速 串口控制 按键控制

    难点:1.串口定时器T1,和T0定时器优先级 2.pwm频率与占空比的设置 按键控制 按键1——前进 按键2——后退 按键3——加速 按键4——减速 (板子上只有四个按键) 串口控制 ‘1’——前进 ...