整合Spring3+Struts2+Hibernate4
一、整合Spring3.2.12
1. 将Spring3.2.12的配置文件加入到src/main/resources中
spring.xml
config.properties
2. 在pom.xml中加入Spring3.2.12的依赖jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.12.RELEASE</version>
</dependency>
3. 在web.xml中配置Spring的监听器ContextLoaderListener,注意此时没有加入hibernate的配置文件spring-hibernate.xml
<!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
<!-- <param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value> -->
</context-param>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4. 将log4j的配置文件加入到src/main/resources中
config.properties
在pom.xml中加入log4j的依赖jar包
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
5. 在pom.xml中加入junit的依赖jar包
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
6. 测试Spring IOC
略
二、整合Struts2.3.10.1
1. 将Struts2.3.10.1的配置文件加入到src/main/resources中
struts.xml
2. 在pom.xml中加入Struts2.3.10.1的依赖jar包
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.20.1</version>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.20.1</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.20.1</version>
</dependency>
3. 在web.xml中配置Struts2的核心控制器
<!-- Struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
4. 测试Struts2 Action
略
三、整合Hibernate4.2.16
1. 将Hibernate4.2.16配置文件加入到src/main/resources中
spring-hibernate.xml
2. 修改Spring的ContextLoaderListener配置,将spring-hibernate.xml加入到被Spring解析的配置文件之列
<!-- spring配置文件位置 -->
<context-param>spring-hibernate.xml
<param-name>contextConfigLocation</param-name>
<!-- <param-value>classpath:spring.xml</param-value> -->
<param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>
</context-param>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3. 在pom.xml中加入Hibernate4.2.16的依赖jar包
<!-- hibernate4 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.16.Final</version>
</dependency>
3. 在web.xml中配置openSessionInView,让Spring来管理Session,但是这个Filter必须放在Struts2的StrutsPrepareAndExecuteFilter之前
<!-- openSessionInView配置 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
4. 在pom.xml中配置数据源druid、mysql数据库驱动、aspectjweaver的依赖jar包
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.13</version>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
5. 测试Hibernate Session
略
四、综合测试
客户端浏览器访问action
通过action的依赖对象访问userService的save方法
通过userService的依赖对象访问userDao的save方法
最后查看则注册用户是否成功
成功跳转到注册成功页面regSuccess.jsp
失败跳转到注册失败页面regFail.jsp
整合SSH之详细步骤
猜你喜欢
转载自javawangbaofeng.iteye.com/blog/2222689
今日推荐
周排行