近期集成了一次较新版本的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)的更多相关文章

  1. 转: javascript模块加载框架seajs详解

    javascript模块加载框架seajs详解 SeaJS是一个遵循commonJS规范的javascript模块加载框架,可以实现javascript的模块化开发和模块化加载(模块可按需加载或全部加 ...

  2. Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)

    当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的坏习惯. 通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车 ...

  3. Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)

    通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...

  4. (转) Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)

    http://blog.csdn.net/u010648555/article/details/60767633 当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的 ...

  5. Hadoop 新 MapReduce 框架 Yarn 详解

    Hadoop 新 MapReduce 框架 Yarn 详解: http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ Ap ...

  6. 测试框架mochajs详解

    测试框架mochajs详解 章节目录 关于单元测试的想法 mocha单元测试框架简介 安装mocha 一个简单的例子 mocha支持的断言模块 同步代码测试 异步代码测试 promise代码测试 不建 ...

  7. Android热门网络框架Volley详解[申明:来源于网络]

    Android热门网络框架Volley详解[申明:来源于网络] 地址:http://www.cnblogs.com/caobotao/p/5071658.html

  8. 【python3+request】python3+requests接口自动化测试框架实例详解教程

    转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...

  9. python+requests接口自动化测试框架实例详解

    python+requests接口自动化测试框架实例详解   转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实 ...

随机推荐

  1. 使用模版引擎填充重复dom元素

    引入arttemplate,定义newajax发送跨域请求获得数据,将获得的数据用定义的格式渲染 <!DOCTYPE html><html lang="en"&g ...

  2. IOS--UIButton的使用方法

    设置UIButton的文字显示位置.字体的大小.字体的颜色 分类: iphone界面详解2012-12-21 14:32 27269人阅读 评论(2) 收藏 举报 btn.frame = CGRect ...

  3. 三种常见字符编码简介:ASCII、Unicode和UTF-8

    什么是字符编码? 计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个字节能表示的最大的整数就是255( ...

  4. 零零碎碎搞了一天最后发现是ruby版本问题

    查来查去查不到问题,后来在stackoverflow看到: http://stackoverflow.com/questions/22352838/ruby-gem-install-json-fail ...

  5. Android Wear开发 - 数据通讯 - 第三节 : 事件处理

    http://developer.android.com/training/wearables/data-layer/events.html 以下是本人在学习官方开发文档时的笔记,主要是翻译为主,并在 ...

  6. NodeJS制作爬虫全过程

    这篇文章主要介绍了NodeJS制作爬虫的全过程,包括项目建立,目标网站分析.使用superagent获取源数据.使用cheerio解析.使用eventproxy来并发抓取每个主题的内容等方面,有需要的 ...

  7. Git skills in reseting files

    How to uncommit files that were committed long time a ago?Note: Since all changes in the current wor ...

  8. AlgorithmsI Exercises: Analysis of Algorithms

    Question 1 Suppose that you time a program as a function of N and producethe following table. N seco ...

  9. phpMyAdmin import.php 安全漏洞

    漏洞名称: phpMyAdmin import.php 安全漏洞 CNNVD编号: CNNVD-201307-068 发布时间: 2013-07-05 更新时间: 2013-07-05 危害等级:   ...

  10. socket(tcp)互发信息

    一:有图有真相,很简单 a, b, Thread 构造函数(ParameterizedThreadStart)初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托. 参数star ...