黑马程序员_Spring_05

本文详细介绍Spring2.5、Hibernate3.3与Struts1.3/Struts2的整合开发流程,包括所需JAR包、配置文件示例及解决常见问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-----------android培训java培训、期待与您交流!-----------

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

 

 

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

<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">

       <propertyname="sessionFactory" ref="sessionFactory"/>

</bean>

<tx:annotation-driventransaction-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-mappingpackage="cn.itcast.bean">

   <class name="Person" table="person">

       <id name="id" type="integer" >

            <generatorclass="native"/>

       </id>

       <property name="name" length="10"not-null="true"/>

   </class>

</hibernate-mapping>

 

Hibernate二级缓存的配置

<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource"/>

          <propertyname="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>

 

 

在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"/>

在struts配置文件中添加进spring的请求控制器,该请法语控制器会先根据action的path属性值到spring容器中寻找跟该属性值同名的bean。如果寻找到即使用该bean处理用户请求

<controller>

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

</controller>


Hibernate二级缓存的配置

<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource"/>

          <propertyname="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>

 

 

Ehcache默认的配置文件ehcache.xml(放在类路径下)

<ehcache>

   <diskStore path="D:\cache"/>

   <defaultCache maxElementsInMemory="1000“ eternal="false“ overflowToDisk="true"

       timeToIdleSeconds="120"

       timeToLiveSeconds="180"

       diskPersistent="false"

       diskExpiryThreadIntervalSeconds="60"/>

<cachename="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 指定专门用于清除过期对象的监听线程的轮询时间

 

 

在需要缓存的实体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-mappingpackage="cn.itcast.bean">

   <class name="Person" table="person">

             <cacheusage="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指定缓存的区域名

 

 

使用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>

 


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

使用到struts2的lib目录下所有不带-plugin结尾的jar文件,但除了struts2-spring-plugin-2.0.11.1.jar

 

 

在web容器中实例化spring容器和配置struts2

<!-- 指定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>

配置struts2

  <filter>

       <filter-name>struts2</filter-name>

       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

   </filter>

   <filter-mapping>

       <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

  </filter-mapping>

 

 

struts2的配置文件模版struts.xml

<?xml version="1.0"encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

   "-//Apache Software Foundation//DTD Struts Configuration2.0//EN"

   "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

  <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。

    如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。-->

   <constant name="struts.action.extension"value="do"/>

   <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭-->

   <constant name="struts.serve.static.browserCache"value="false"/>

   <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开-->

   <constant name="struts.configuration.xml.reload"value="true"/>

   <!-- 开发模式下使用,这样可以打印出更详细的错误信息-->

   <constant name="struts.devMode" value="true"/>

    <!-- 默认的视图主题-->

   <constant name="struts.ui.theme" value="simple"/>

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

        <package name="person"namespace="/person" extends="struts-default">

                 <global-results>

                           <resultname="message">/WEB-INF/page/message.jsp</result>

                 </global-results>

                   <actionname="action_*" class="personList"method="{1}">

                            <resultname="list">/WEB-INF/page/persons.jsp</result>

                            <resultname="add">/WEB-INF/page/add_person.jsp</result>

                            <resultname="edit">/WEB-INF/page/edit_person.jsp</result>

                   </action>

   </package>

</struts>

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值