近期集成了一次较新版本的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. Android获取相册图片

    1. AlertDialog的使用 2. 显示和隐式意图的区别 3. 相册页面的跳转 4. 选择完成后返回图片的获取 ----------------------------------------- ...

  2. Why your Games are Unfinished, and What To Do About It (转)

    So, you've got a new game idea, and it's going to change what everyone knows about the genre! Great! ...

  3. 串行CPU设计

    一.概述 串行CPU工作流程 串行CPU的时序流程如下图所示:取指.译码.执行.回写. 其中,取指.回写是与存储器打交道:而译码与执行则是CPU内部自个儿的操作. 我们究竟想要CPU干什么?     ...

  4. JSP HTML区别

    1.最简单的区别就是,HTML能直接打开,jsp只能发布到Tomact等服务器上才能打开2.定义上HTML页面是静态页面可以直接运行,JSP页面是动态页它运行时需要转换成servlet.3.他们的表头 ...

  5. program_options禁止命令行短参数

    典型的 boost program_options的用法如下: #include <boost/program_options.hpp> using namespace boost::pr ...

  6. Unity问答——请教一下NGUI的图片转换问题

    这篇博客源自我在泰课在线的回答.链接:http://www.taikr.com/group/1/thread/92 问:请教一下NGUI的图片转换问题 1.NGUI能不能把导入的方形图片转成圆形的(因 ...

  7. Javascript 注意点

    prototype有助于减少function的冲突. 闭包有助于避免全部变量. this, prototype有助于实例化多个对象. 函数 函数表达式

  8. [原创]leet code - path sum

    ;            ;                ;                            }        }        ;            }};

  9. Delphi控件的显示内容与显示边框是两回事

    没有内容,不代表没有边框.比如设计期一个空的TImage仍是有边框的. if (csOpaque in image1.ControlStyle) then ShowMessage('不透明')else ...

  10. bzoj3339 bzoj3585

    两题本质是一样,只不过3585要离散化这种不修改,不强制的问题,显然先考虑离线算法这道题的思路和bzoj1878非常像考虑到如果只是求每个前缀的mex,我们是很容易扫一遍就得出来的我们设为这个位置的m ...