1. 搭建Struts2 环境

  1. 创建 struts2 的配置文件: struts.xml;
  2. 在 web.xml 中配置 struts2 的核心过滤器;
// 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="demo" namespace="/" extends="struts-default">
<!-- action 依靠spring框架创建 -->
<action name="customerAction_*" class="customerAction" method="{1}"> </action>
</package>
</struts> // web.xml
<!-- 配置 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>/*</url-pattern>
</filter-mapping>

2. 搭建 Spring 环境

  1. 创建 spring 配置文件: applicationContext.xml;
  2. 在 web.xml 中配置监听器: ContextLoaderListener;
// applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/binding/soap"
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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://cxf.apache.org/binding/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 配置action 对象 -->
<bean id="customerAction" class="cn.itcast.web.action.CustomerAction" scope="prototype"/> </beans> // web.xml
<!-- 配置Spring的核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 加载方式:默认只能加载 WEB-INF 目录下的配置文件;
通过上下文参数指定spring配置文件,加载 src 目录下配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

3. 搭建 Hibernate 环境

  1. 创建 hibernate 配置文件: hibernate.cfg.xml;
// hibernate.cfg.xml
<!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>
<!-- 数据库四大参数和方言 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/hibernate_day01
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property> <property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property> <!-- 为了方便调试是否在运行hibernat时,在日志中输出sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否在日志中对输出的 sql 语句格式化 -->
<property name="hibernate.format_sql">true</property> <!-- 通过映射文件,生成数据库表结构 -->
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>

4. SSH 框架整合

4.1 Struts2 和 Spring 整合

  1. action 对象创建交给 Spring 创建;注意,action是多例的,配置scope="prototype"
  2. 创建 action 类;
  3. 将 action 对象配置到 spring 配置文件中;
  4. 在 struts.xml 中, action节点的class属性配置为spring工厂中action对象的bean的id;

4.2 Hibernate 和 Spring 整合

  1. 数据源dataSource, 交给 Spring;
  2. SessionFactory 对象创建交给 Spring;
  3. 事务管理交给 Spring 管理;
// db.properties, 存放数据库四大参数
// "jdbc."表示前缀,是任意的,为了和其他参数区别;
// "user"在Linux系统下有问题,"username"在 windows 下有问题
jdbc.driverClass = com.mysql.jdbc.driver
jdbc.jdbcUrl = jdbc:mysql://localhost:3306/maven
jdbc:user = root
jdbc:password = root // applicationContext.xml
<!-- 加载属性文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!-- 配置dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- XML 方式管理事务 -->
<!-- 配置通知:具体增强逻辑 -->
<tx:advice id="txAdvice">
<tx:attributes>
<!-- 匹配业务类中方法名称 -->
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice> <!-- 配置 AOP -->
<aop:config>
<!-- 配置切点: 具体哪些方法需要增强(真正被增强的方法) -->
<aop:pointcut expression="execution(* cn.itcast.service.*.*(..))" id="cut"/>
<!-- 配置切面: 将增强的逻辑作用到切点(通知+切入点) -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="cut"/>
</aop:config> <!-- 注解方式管理事务 -->
<!-- 1. 开启注解扫描, 2. 在service类上或者方法上使用注解 @Transactional -->
<tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置 action 对象 -->
<bean id="customerAction" class="cn.itcast.web.action.CustomerAction" scope="prototype"/>

4.3 具体实现

  1. 创建Customer实体类,映射文件,将映射文件引入Hibernate核心配置文件;
  2. 创建 action,service,dao, 完成注入
    • 在类中添加相应属性,并生成 set 方法;
    • 在 spring 配置文件中完成注入;
  3. 在 struts.xml 中配置 action,以及结果视图;

参考资料

SSH 框架整合总结的更多相关文章

  1. SSH框架整合

    SSH框架整合 一.原理图 action:(struts2) 1.获取表单的数据 2.表单的验证,例如非空验证,email验证等 3.调用service,并把数据传递给service Service: ...

  2. dwr与ssh框架整合教程

    (1)dwr与ssh框架整合教程dwr框架介绍. DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开 发人员开发包含AJ ...

  3. ssh框架整合之登录以及增删改查

    1.首先阐述一下我用得开发工具,myeclipse2017+oracle,所以我的基本配置步骤可能不一样,下面我用几张图来详解我的开发步骤. ---1先配置structs (Target 选择apac ...

  4. Spring+Hibernate+Struts(SSH)框架整合

    SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...

  5. J2EE进阶(十)SSH框架整合常见问题汇总(一)

    SSH框架整合常见问题汇总(一) 前言 以下所列问题具有针对性,但是遇到同类型问题时均可按照此思路进行解决. HTTP Status 404 - No result defined for actio ...

  6. MVC+Spring.NET+NHibernate .NET SSH框架整合 C# 委托异步 和 async /await 两种实现的异步 如何消除点击按钮时周围出现的白线? Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法

    MVC+Spring.NET+NHibernate .NET SSH框架整合   在JAVA中,SSH框架可谓是无人不晓,就和.NET中的MVC框架一样普及.作为一个初学者,可以感受到.NET出了MV ...

  7. SSH框架整合的其它方式

    --------------------siwuxie095 SSH 框架整合的其它方式 1.主要是整合 Spring 框架和 Hibernate 框架时,可以不写 Hibernate 核心配置文件: ...

  8. SSH框架整合过程总结

    ---------------------siwuxie095                                 SSH 框架整合过程总结         (一)导入相关 jar 包(共 ...

  9. SSH框架整合思想

    --------------------siwuxie095                                 SSH 框架整合思想         1.SSH 框架,即 Struts2 ...

  10. SSH框架整合jar包时的注意事项

    SSH框架整合jar包时的注意事项: 在将三个框架所需的jar整合到一起后,要看一下有没有相同类型但是版本不同的jar包,如果有的话,需要把低版本的jar包删除掉,否则会报错.我这里整合的时候java ...

随机推荐

  1. 0051 MyBatis关联映射--多对多关系

    用户与订单时一对多关系,再加上商品信息的话,订单与商品之间就是多对多关系了 DROP DATABASE IF EXISTS testdb; USE testdb; /*用户表,记录用户信息:用户与订单 ...

  2. PHP学习笔记(3)GD库画图

    <?php //加header头,不然浏览器乱码 header("content-type: image/png"); //创建画布资源 $img = imagecreate ...

  3. linux学习笔记30--命令at和crontab

    在windows系统中,windows提供了计划任务这一功能,在控制面板 -> 性能与维护 -> 任务计划, 它的功能就是安排自动运行的任务. 通过'添加任务计划'的一步步引导,则可建立一 ...

  4. linux学习笔记26--命令wc

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的行数.字数.字节数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的行数.字 ...

  5. 跟着百度学PHP[17]-PHP扩展CURL的模拟登陆并获取数据

    这两天也不知道怎么,学习效率低.很无奈. 如何知道要去URL该怎么填写呢?就是填写表单中的Action内容: tempnam() 函数创建一个具有唯一文件名的临时文件. <?php header ...

  6. 1.2.3 Task and Back Stack - 任务和回退堆

    一个应用通常包含多个Activities.每个activity的设计应该围绕着某种指定类型的action,如果这样做了,用户就可以执行该action,也可以用它来开启另外的activity.例如,邮件 ...

  7. 推广Facebook技巧

    1.创建有吸引力的内容发布多样化的内容,包括图片,状态更新,视频,投票等.可以问你的粉丝一些问题让他们提供答案.这些内容不仅你的粉丝可以看到,它们还将会出现在你粉丝的个人动态栏,所以他们的朋友也是可以 ...

  8. Angular4中的依赖注入

    在Angular中使用依赖注入,可以帮助我们实现松耦合,可以说只有在组件中使用依赖注入才能真正 的实现可重用的组件. 如果我们有个服务product.service.ts,其中export了一个Pro ...

  9. python zlib压缩存储到mysql列

    数据太大压缩存储,可以使用zlib中的压缩函数,代码如下: import ujson import MySQLdb import zlib import base64 kwargs = { 'host ...

  10. 移动端上下滑动事件之--坑爹的touch.js

    原文   http://blog.csdn.net/minidrupal/article/details/39611605 移动端页面的盛行,微信的便利的页面推广等等,让越来越多的css3效果和htm ...