SSH整合

新建一个动态web工程-->加入Spring-->加入Hibernate-->加入Struts2

1.在 web中应用Spring
  目的:在web应用程序加载成功之后,就可以使用Spring IOC容器
  (1)加入jar包
    除了Spring所必须的jar包之外,还有两个jar包必须加入
    spring-webmvc-4.0.0.RELEASE.jar        spring-web-4.0.0.RELEASE.jar
  (2)在web.xml文件中,Spring提供的ContextLoaderListener启动Spring IOC容器
    <context-param>
    <!-- 在当前web应用的初始化参数中配置Spring配置文件的路径 -->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
2.加入Spring
  (1)加入jar包
    包括Spring所必须的jar包,还有配置数据源所需的jar包
  (2)在类路径下创建Spring的配置文件applicationContext.xml,在配置文件中,配置数据源和Spring的声明式事务
    <!-- 配置数据源,数据源的属性在外部资源文件中设置 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
      <property name="user" value="${jdbc.user}"></property>
      <property name="password" value="${jdbc.password}"></property>
      <property name="jdbcUrl" value="${jdbc.url}"></property>
      <property name="driverClass" value="${jdbc.driverClass}"></property>
      <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
      <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>
    <!-- 配置Spring的声明式事务 -->
    <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="hibernateTransactionManager">
      <property name="dataSource" ref="dataSource"></property>
      <property name="sessionFactory" ref="localSessionFactoryBean"></property><!-- ref属性的值是加入Hibernate时配置的SessionFactory的bean的id -->
    </bean>
    <tx:advice id="adviceDjp" transaction-manager="hibernateTransactionManager">
      <tx:attributes>
        <tx:method name="get*" read-only="true"/>
        <tx:method name="*"/>
      </tx:attributes>
    </tx:advice>
    <aop:config>
      <aop:pointcut expression="execution(* *.*(..))" id="pointcutDjp"/>
      <aop:advisor advice-ref="adviceDjp" pointcut-ref="pointcutDjp"/>
    </aop:config>
3.加入Hibernate
  (1)在类路径下创建Hibernate的配置文件hibernate.cfg.xml,配置Hibernate的一些基本信息
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
  (2)创建持久化类以及对应的对象-关系映射文件(.hbm.xml)
  (3)在Spring的配置文件applicationContext.xml中配置SessionFactory实例
    <!-- 配置sessionFactory -->
    <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="localSessionFactoryBean">
      <property name="dataSource" ref="dataSource"></property>
      <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
      <property name="mappingLocations" value="classpath:com/…/…/*.hbm.xml"></property>
    </bean>
4.加入Struts2
  (1)加入jar包
    除了Struts2所必须的jar包之外,还有一个jar包必须加入 struts2-spring-plugin-2.5.12.jar
  (2)在web.xml文件中加入Struts2的Filter
    <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>/*</url-pattern>
    </filter-mapping>
  (3)创建Action类,并在Spring IOC容器中配置Struts2的Action实例
    <bean class="……" id="……" scope="prototype">
      ……
    </bean>
    注意:由于Struts2的Action不是单例的,所以,在applicationContext.xml中配置时,必须修改scope属性(该属性默认singleton)
  (4)在类路径下创建Struts2 的配置文件struts.xml
    <struts>
      <constant name="struts.enable.DynamicMethodInvocation" value="false" />
      <constant name="struts.devMode" value="true" />
      <package name="default" namespace="/" extends="struts-default">
        <action name="……" class="……" method="……">
          <result name="……">……</result>
        </action>
      </package>
    </struts>
    注意:struts.xml中<action>的class属性的值是applicationContext.xml中对应的<bean>的id属性的值

SSH整合的详细步骤的更多相关文章

  1. SSH框架搭建详细步骤整理

    学习Java面前有两座山,一座山叫SSM,一座山叫SSH,跨越了这两座山之后才能感受到这个语言的魅力所在,SSM框架的搭建详细在之前博客已经涉及了,今天来整理SSH框架详细步骤: 生有涯 而 学无涯 ...

  2. SSH三大框架整合配置详细步骤(3)

    5 配置Spring2.5 5.1 基础配置 1)        导入spring包.下载spring-framework-2.5.6并解压后,在spring-framework-2.5.6" ...

  3. SSH三大框架整合配置详细步骤(2)

    4 配置Hibernate Hibernate MySql连接配置 在Hibernate中,可以配置很多种数据库,例如MySql.Sql Server和Oracle,Hibernate MySql连接 ...

  4. SSH三大框架整合配置详细步骤(1)

    配置Struts2.0 3.1 基础配置 1)引入Struts必需的五个jar包.下载struts-2.1.6-all.zip解压后,struts-2.1.6\lib目录下是struts所有的相关ja ...

  5. Spring整合Hibernate详细步骤

    阅读目录 一.概述 二.整合步骤 回到顶部 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使 ...

  6. Ubuntu以及CentOS7修改ssh端口号详细步骤

    1.Ubuntu修改ssh端口号步骤: 1.修改sshd.config文件.执行vim etc/ssh/sshd_config.增加上我们需要增加的ssh的端口号.图例增加了5309的端口号. ESC ...

  7. SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码)

    准备工作 环境准备 JAVA版本 java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121 ...

  8. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSM(Spring+Spring MVC+MyBatis)框架整合搭建详细步骤

    因为 Spring MVC 是 Spring 框架中的一个子模块,所以 Spring 与 SpringMVC 之间不存在整合的问题.实际上,SSM 框架的整合只涉及 Spring 与 MyBatis ...

  9. IDEA整合Junit详细步骤

    一.添加Junit插件. 1.file-->setting-->plugins-->搜索Junit-->安装插件(一般已默认安装,无需手动安装). 二.设置Junit测试参数: ...

随机推荐

  1. Mybatis第七篇【resultMap、resultType、延迟加载】

    resultMap 有的时候,我们看别的映射文件,可能看不到以下这么一段代码: <resultMap id="userListResultMap" type="us ...

  2. oracle 删除外键约束 禁用约束 启用约束

    oracle 删除外键约束 禁用约束 启用约束 执行以下sql生成的语句即可 删除所有外键约束 Sql代码  select 'alter table '||table_name||' drop con ...

  3. linux kill 命令

    kill 命令的用途 kill 命令很容易让人产生误解,以为它仅仅就是用来杀死进程的.我们来看一下 man page 对它的解释:kill - send a signal to a process. ...

  4. 一个基于Asp.net MVC的博客类网站开源了!

    背景说明: 大学时毕业设计作品,一直闲置在硬盘了,倒想着不如开源出来,也许会对一些人有帮助呢,而且个人觉得这个网站做得还是不错了,毕竟是花了不少心思,希望对你有所帮助. github地址:https: ...

  5. [Sdoi2010]星际竞速

    个人对山东省选已经十分无语了,做了三道题,都TM是费用流,这山东省选是要干什么,2009--2011连续三年,只要会费用流,然后建个边,跑一跑就过了. 10 年一度的银河系赛车大赛又要开始了.作为全银 ...

  6. VC维含义的个人理解

    有关于VC维可以在很多机器学习的理论中见到,它是一个重要的概念.在读<神经网络原理>的时候对一个实例不是很明白,通过这段时间观看斯坦福的机器学习公开课及相关补充材料,又参考了一些网络上的资 ...

  7. CSS3D模型

    html部分 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

  8. 【笔记】如何查看HTTP请求头&&【实验吧】天下武功唯快不破

    打开Chrome浏览器,点击右上角“三”按钮. 点击工具-----再点击开发者工具   找到Network选项框.以百度经验页面为例,点击任务选框来查看网络请求流   在Network框内会有所有的请 ...

  9. Hexo + GitHub Pages搭建博客

    搭建 Node.js 环境 为什么要搭建 Node.js 环境? – 因为 Hexo 博客系统是基于 Node.js 编写的 Node.js 是一个基于 Chrome V8 引擎的 JavaScrip ...

  10. VTL(Velocity Templates Language,即Velocity模板语言)初识语法总结

    1.velocity是一门基于Java语言的视图表现层模板引擎,它可以取代jsp,比jsp更高效. 2.velocity变量的定义与引用 (1).定义一个变量:#set ($a = "vel ...