S2SH框架集成详解(Struts 2.3.16 + Spring 3.2.6 + Hibernate 3.6.10)
近期集成了一次较新版本的s2sh,出现了不少问题,网上资料也是良莠不齐,有的甚至就是扯淡,简单的把jar包扔进去就以为是集成成功了,在这里整理一下详细的步骤,若哪位有什么不同看法,可以留言,欢迎批评改正。
首先说下集成的环境,Eclipse 4.4,Tomcat 7.0.54,Struts2.3.16,Hibernate3.6.10,Spring3.2.6
相关JAR包已上传至百度云:http://pan.baidu.com/s/1pJluA5l
- 搭建Struts2框架
1. 创建Java Web工程SSH
2. 添加struts2.3.16的8个基本类库,可能以前版本是6个,但是2.3.16版本本人测试是8个,大家可以自己测试下

3. 在src下创建config.struts包,并创建struts2的配置文件struts.xml
4. 在web.xml文件中添加过滤器,如果strtus2配置文件放在src下无需添加init-param标签。
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>
struts-default.xml,struts-plugin.xml,config/struts/struts.xml
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
5. 向struts.xml中添加如下代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="ssh" namespace="/" extends="struts-default"> <action name="" class="" method="">
<result name=""></result>
</action> </package>
</struts>
6. 创建以下路径用于测试
WebContent/Web-INF/jsp/index.jsp
(Web-INF为安全目录,无法直接访问)
7. 在src目录下创建action包,用于放置Struts2的Action,然后创建MainAction,用于测试,以及以后的页面跳转,并添加如下代码:
package action;
public class MainAction {
public String start(){
return "start";
}
}
8. 在struts.xml中进行配置
<struts>
<package name="ssh" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index" class="action.MainAction" method="start">
<result name="start">WEB-INF/jsp/index.jsp</result>
</action>
</package>
</struts>
9. 启动web服务器,测试http://localhost:8080/SSH/
- 集成struts2和spring
1. 添加spring类库

2. 添加strut2集成spring类库:struts2-spring-plugin-2.3.16.3.jar
3. 添加spring配置文件applicationContext.xml,并添加如下信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> </beans>
4. 在web.xml文件中添加spring监听器,在filter标签外,context-param标签是为了自定义spring配置文件的位置
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/applicationContext.xml </param-value>
</context-param>
5. 启动Tomcat测试
6. spring管理struts2中Action,在spring文件中配置
<bean id="mainAction" class="action.MainAction"></bean>
7. 修改struts对应配置文件信息,换成spring配置文件中<bean id="mainAction">
<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="ssh" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index" class="mainAction" method="start">
<result name="start">WEB-INF/jsp/index.jsp</result>
</action>
</package>
</struts>
8. 重启tomcat测试是否可用
- 集成hibernate
1. 添加hibernate必须的jar包

2. 添加hibernate配置文件,位置设置在config/hibernate/cfg目录下,文件名:hibernate.cfg.xml,并向其中添加如下配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory></session-factory>
</hibernate-configuration
3. 创建包hibernate.hbm,用于存放hibernate的映射文件,创建User.hbm.xml并添加如下信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
</hibernate-mapping>
4. 配置spring的数据源,添加Tomcat JDBC POOL连接池类库

5. 在spring配置文件中配置连接池,在src目录下创建properties包,用于存属性文件,并创建文件db.properties,用于保存数据库连接池的配置属性之后向db.properties中添加数据库连接池的属性数据,包括:url、driver、username、password、initialSize、maxActive、maxIdle、minIdle、maxWait、removeAbandoned、 removeAbandonedTimeOut等
<bean id="PropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/properties/datasource.properties" />
</bean> <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" >
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" /> <property name="initialSize" value="${initialSize}" />
<property name="maxActive" value="${maxActive}" />
<property name="maxIdle" value="${maxIdle}" />
<property name="minIdle" value="${minIdle}" />
<property name="maxWait" value="${maxWait}" />
<property name="removeAbandoned" value="${removeAbandoned}" />
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
</bean>
6. 在spring中完全管理hibernate配置,删除hibernate.cfg.xml,在spring配置文件中创建id为sessionFactory的bean
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:config/hibernate/User.hbm.xml</value>
</list>
</property>
</bean>
7. 创建User实体类,UserDao、jsp等,并在spring中配置提供注入,进行测试
附:sping事务配置(基于tx、aop声明式)
1. 在spring配置文件中添加声明
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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
classpath:/org/springframework/beans/factory/xml/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
classpath:/org/springframework/context/config/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
classpath:/org/springframework/transaction/config/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
classpath:/org/springframework/aop/config/spring-aop-3.2.xsd">
2. 添加事务配置,bean标签之外,beans标签之内
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="false"/>
<tx:method name="add*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* action.*.*(..))"/>
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/>
</aop:config>
S2SH框架集成详解(Struts 2.3.16 + Spring 3.2.6 + Hibernate 3.6.10)的更多相关文章
- 转: javascript模块加载框架seajs详解
javascript模块加载框架seajs详解 SeaJS是一个遵循commonJS规范的javascript模块加载框架,可以实现javascript的模块化开发和模块化加载(模块可按需加载或全部加 ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的坏习惯. 通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车 ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)
通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...
- (转) Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
http://blog.csdn.net/u010648555/article/details/60767633 当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的 ...
- Hadoop 新 MapReduce 框架 Yarn 详解
Hadoop 新 MapReduce 框架 Yarn 详解: http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ Ap ...
- 测试框架mochajs详解
测试框架mochajs详解 章节目录 关于单元测试的想法 mocha单元测试框架简介 安装mocha 一个简单的例子 mocha支持的断言模块 同步代码测试 异步代码测试 promise代码测试 不建 ...
- Android热门网络框架Volley详解[申明:来源于网络]
Android热门网络框架Volley详解[申明:来源于网络] 地址:http://www.cnblogs.com/caobotao/p/5071658.html
- 【python3+request】python3+requests接口自动化测试框架实例详解教程
转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...
- python+requests接口自动化测试框架实例详解
python+requests接口自动化测试框架实例详解 转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实 ...
随机推荐
- Android编译输出 资源指定AAPT
BoardConfigCommon.mk # Default Command lineSEC_DEV_APP_LOCAL_AAPT_FLAGS := -c sw360dpSEC_DEV_FRAMEWO ...
- 如何编写一个简单的makefile
一个规则的构成 目标:依赖1,依赖2······ 命令 例子: objs := init.o nand.o head.o main.o nand.bin : $(objs) arm-linux-ld ...
- Java中传参的值传递和引用传递问题(转)
今天遇到了一个java程序,需要用参数来返回值(虽然最后用另一种方法实现了),在网上看到这样一篇文章,很受启发. 本文章来自于http://hi.baidu.com/xzhilie/blog/item ...
- ExtJS 4 类系统
ExtJS 4的类系统(class system)进行了一次重大重构,ExtJS4的新架构就是基于这套新的类系统构建的,因此有必要先了解以下这个class system这篇文章分为四章 I: &quo ...
- VmWare问题解决(网络变更后虚拟主机无法上网)
安装 Vmware,并新建一个虚拟机后,当时做好配置(NAT模式)后,虚拟机能够正常上网. 然后将电脑带到另一个地理位置办公时,却无法上网. 本篇探讨问题的缘由和几种解决方式. 一.缘由解析 仅探讨上 ...
- W3C vs. WHATWG HTML5 Specs – The Differences Documented
A few weeks ago, HTML5 became an official W3C Recommendation. I took advantage of this event to disc ...
- 检查网口流量与前10名流量大IP
此脚本包含的功能有: 1.实时监控任意网卡的流量 2.统计10秒内平均流量 3.统计每个端口在10秒内的平均流量,基于客户端和服务端端口统计.可以看出哪些端口占流量比较大,对于web服务器,一般是80 ...
- Java工程转换为Maven工程
1. 前言 在开发中经常要建立一个Maven的子工程,对于没有模板的同学来说从Java工程来转换也是一个不错的选择.本文就如何从一个Java工程创建一个Maven工程做了一个介绍,相信对于将一个Jav ...
- PlatformTransactionManager
Spring Boot 使用事务非常简单,首先使用注解 @EnableTransactionManagement 开启事务支持后,然后在访问数据库的Service方法上添加注解 @Transactio ...
- 【HDOJ】3442 Three Kingdoms
bfs+状态压缩.初始化数组的曼哈顿距离条件写错了,改了一下午. /* 3442 */ #include <iostream> #include <cstdio> #inclu ...