Spring2.5+Hibernate3.3+Struts1.3整合开发

hibernate核心安装包下的:
hibernate3.jar
lib/required/*.jar
lib/optional/ehcache-1.2.3.jar
hibernate 注解安装包下的
lib/test/slf4j-log4j12.jar


Spring安装包下的
dist/spring.jar
dist/modules/spring-webmvc-struts.jar
lib/jakarta-commons/commons-logging.jar、commons-dbcp.jar、commons-pool.jar
lib/aspectj/aspectjweaver.jar、aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
lib/j2ee/common-annotations.jar
lib/log4j/log4j-1.2.15.jar


Struts
下载struts-1.3.8-lib.zip,需要使用到解压目录下的所有jar,建议把jstl-1.0.2.jar和standard-1.0.2.jar更换为1.1版本。Spring中已经存在一个antlr-2.7.6.jar,所以把struts中的antlr-2.7.2.jar删除,避免jar冲突。
数据库驱动jar

 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
 ..... 略 </bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="dataSource" ref="dataSource"/>
     <property name="mappingResources">
     <list>
       <value>cn/itcast/bean/Person.hbm.xml</value>
     </list>
     </property>
     <property name="hibernateProperties">
     <value>
         hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
         hibernate.hbm2ddl.auto=update
         hibernate.show_sql=false
         hibernate.format_sql=false
       </value>
    </property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>


实体bean配置模版.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.bean">
    <class name="Person" table="person">
        <id name="id" type="integer" >
            <generator class="native"/>
        </id>
        <property name="name" length="10" not-null="true"/>
    </class>

</hibernate-mapping>

Hibernate二级缓存的配置

 

 

在需要缓存的实体bean配置文件中加入缓存配置项


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.bean">
    <class name="Person" table="person">
     <cache usage="read-write" region="cn.itcast.bean.Person"/>
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name" length="10" not-null="true"/>
    </class>
</hibernate-mapping>

usage说明了缓存的策略,region指定缓存的区域名

 

Ehcache默认的配置文件ehcache.xml(放在类路径下)
<ehcache>
    <diskStore path="D:/cache"/>
    <defaultCache  maxElementsInMemory="1000“  eternal="false“ overflowToDisk="true"
        timeToIdleSeconds="120"
        timeToLiveSeconds="180"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="60"/>
<cache name="cn.itcast.bean.Person" maxElementsInMemory="100" eternal="false"
    overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false"/>
</ehcache>

    defaultCache节点为缺省的缓存策略
     maxElementsInMemory 内存中最大允许存在的对象数量
     eternal 设置缓存中的对象是否永远不过期
     overflowToDisk 把溢出的对象存放到硬盘上
     timeToIdleSeconds 指定缓存对象空闲多长时间就过期,过期的对象会被清除掉
     timeToLiveSeconds 指定缓存对象总的存活时间
     diskPersistent 当jvm结束是是否持久化对象
     diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间

在web容器中实例化spring容器

<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

在web容器中配置struts

<servlet>
 <servlet-name>struts</servlet-name>
 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
 <init-param>
  <param-name>config</param-name>
  <param-value>/WEB-INF/struts-config.xml</param-value>
 </init-param>
 <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
 <servlet-name>struts</servlet-name>
 <url-pattern>*.do</url-pattern>
</servlet-mapping>

 

如果action没有交给spring管理时,我们通过下面语句获取spring容器实例
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());


把action交给spring管理后,我们可以使用依赖注入在action中注入业务层的bean。确保action的path属性值与bean的名称相同。

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

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

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="dataSource" ref="dataSource"/>
  <property name="mappingResources">
     <list>
       <value>cn/itcast/bean/Person.hbm.xml</value>
     </list>
  </property>
     <property name="hibernateProperties">
     <value>
         hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
         hibernate.hbm2ddl.auto=update
         hibernate.show_sql=false
         hibernate.format_sql=false
         hibernate.cache.use_second_level_cache=true
         hibernate.cache.use_query_cache=false
         hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
       </value>
     </property>
</bean>

在struts配置文件中添加进spring的请求控制器,该请求控制器会先根据action的path属性值到spring容器中寻找跟该属性值同名的bean。如果寻找到即使用该bean处理用户请求
<controller>
 <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>

 

使用spring解决struts1.3乱码问题

<filter>

<filter-name>encoding</filter-name>
 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 <init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
 </init-param>
</filter>
<filter-mapping>
 <filter-name>encoding</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

 

使用spring解决hibernate因session关闭导致的延迟加载例外问题

<filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

</filter-class>
</filter>
<filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值