整合spring2.5+hibernate3+struts2.0

来源:互联网 发布:java 文件上传md5校验 编辑:程序博客网 时间:2024/05/21 09:35

之前有照着网上的博客教程整合了一个比这个更高版本的ssh2,但那时我只是照着做而已啦,对于只自学过strut2的小虾米来说,原理什么的都不懂,所以下定决心要学习一下其它两个框架。经过近一个月的自学过后,我按照视频的做法又来整合一个低版本的(因为视频是前几年的)下面是对我整合的一个记录。

jdk:1.7
tomcat:7.0

1.导入spring jar包
这里写图片描述
蓝色标注的是较新的两个jar包替换,因为jdk是1.7版本所以,这两个jar包就得换成新版本的,这个很重要,不然会报错的(我就出现错误才知道的)。
2.导入hibernate jar 包
这里写图片描述
蓝色的那个jar包,在strut2中也有,要选择最新版本。
3.导入数据库驱动jar包
这个千万别忘记啦
4.编写beans.xml文件
将sessionFactory交给spring管理,并由spring 管理连接数据库

<?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-2.5.xsd           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">     <context:annotation-config/>         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">        <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>        <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>        <property name="username" value="root"/>        <property name="password" value="qazwsx"/>         <!-- 连接池启动时的初始值 -->         <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.hibernate3.LocalSessionFactoryBean">         <property name="dataSource" ref="dataSource"/>         <property name="mappingResources">            <list>              <value>com/sunnie/bean/Person.hbm.xml</value>            </list>         </property>         <property name="hibernateProperties">            <value>                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect                hibernate.hbm2ddl.auto=update                hibernate.show_sql=true                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>    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"/>    </bean>    <tx:annotation-driven transaction-manager="txManager"/>    <bean id="personService" class="com.sunnie.service.impl.PersonServiceBean"/>    <bean id="personList" class="com.sunnie.web.PersonAction"/> </beans>

5.编写bean,service,servicebean
这里写图片描述
6.单元测试
这里写图片描述
7.整合strut2 导入strut2jar包与spring整合sruts2jar包
总体jar包如下,好像挺多的,这里有些包还值得今后研究。
这里写图片描述
这里写图片描述
8.编写web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4"     xmlns="http://java.sun.com/xml/ns/j2ee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    <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>    <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>   <!-- 解决因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>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

9.之后就是写测试啦,struts.xml,action类,jsp页面,吼吼,完成。

这是我整个的项目可以下载哦。

1 0