基于SSH(Struts2.0+Spring2.5+Hibernate3)的框架构建(2)

这一期开始说说SSH的整合

 

SSH的整合非常简单,只要写几句配置就可以实现

1.struts2和spring的整合

他们整合的目的在于把struts的action类的实例化交由spring处理

下面是struts.xml中的关于和spring整合的关键配置

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6.     <constant name="struts.objectFactory" value="spring" />
  7.     <constant name="struts.devMode" value="true" />
  8. <!-- Other configurations -->
  9. </struts>

其中

<constant name="struts.objectFactory" value="spring" />

表示struts的对象交由spring托管

注意:struts.xml文件放置在classes目录下

2.spring和hibernate的整合

由于Spring本身就支持多种持久化框架,因此它内置了各种支持类来支配整合的持久化框架

下面是Spring的配置文件中整合的关键代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:aop="http://www.springframework.org/schema/aop"
  5.     xmlns:tx="http://www.springframework.org/schema/tx"
  6.     xsi:schemaLocation="
  7.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  8.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
  9.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
  10. <!-- SessionFactory -->

  11.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  12.       <property name="configLocation">
  13.           <value>classpath:hibernate.cfg.xml</value>
  14.       </property> 
  15.       <property name="hibernateProperties">
  16.         <value>
  17.           hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
  18.         </value>
  19.       </property>
  20.     </bean>
  21. <!-- transaction -->
  22.     <bean id="transactionManager"
  23.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  24.         <property name="sessionFactory" ref="sessionFactory" />
  25.     </bean>
  26. <!-- Other configurations -->

  27. </beans>
上面的sessionfactory配置中,属性[ configLocation ]将hibernate的配置文件包含进来,在这里,与DB打交道的事情全部交由hibernate去做,Spring只是提供支持类来管理。

注意:Spring配置文件的路径在web.xml中配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="person" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  5.     
  6.     <context-param>
  7.         <param-name>contextConfigLocation</param-name>
  8.         <param-value>WEB-INF/classes/conf/application*.xml</param-value>
  9.     </context-param>
  10. <!-- Other configurations -->
  11. </web-app>

至此,整合完毕

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值