Struts2,Spring3,Hibernate4整合--SSH框架(学习中)

一、包的导入

  1、Spring包

  

  2、Hibernate 包

  

  3、struts 包 (还欠 struts2-spring-plugin-2.3.28.jar 的包)

  

  4、数据库方面的包及junt4的包

  

二、配置文件

  1、beans.xml (具体要注意的已经注释到 xml 中了,目前整合了Spring 与 hibernate4 )

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 打开Spring 的 Annotation 支持-->
<context:annotation-config/>
<!-- 设定Annotation 到 哪里 找-->
<context:component-scan base-package="org.cs"/>
<!-- 打开Spring 的 Aop 代理 -->
<aop:aspectj-autoproxy/> <!-- 使用 DBCP 创建 dateSource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置 hibernate -->
<!-- 配置连接池的初始值 为 1-->
<property name="initialSize" value="1"/>
<!-- 配置最小空闲时 -->
<property name="minIdle" value="1"/>
<!-- 最大连接池 -->
<property name="maxTotal" value="100"/>
<!-- 配置最大空闲时-->
<property name="maxIdle" value="20"/>
<!-- 配置等待时间-->
<property name="maxWaitMillis" value="1000" />
</bean>
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 整合 hibernate4 创建 SessionFactory 工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 设置 Spring 在哪里找 实体类 -->
<property name="packagesToScan">
<value>org.cs.Model</value>
</property>
<!-- 配置 hibernate -->
<property name="hibernateProperties">
<!--<value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>-->
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean> <!-- 配置 Spring 的事务管理 -->
<!-- 创建事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 设置 aop:pointcut 表示 哪些 包.方法 需要加入事务-->
<aop:pointcut id="allMethods"
expression="execution(* org.cs.Service.*.*(..))"/>
<!-- 设置 aop:pointcut 表示 具体 需要加入事物的 方法-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods"/>
</aop:config>
<!-- 配置那些方法需要加入事务处理 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 让所有方法加入事务 -->
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> </beans>

  2、struts.xml (实现 Spring 与 Struts2 的整合 )

<?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>
<!-- 设置 url 后缀-->
<constant name="struts.action.extension" value="action,do"/>
<!-- 当 struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生 产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 表示 Action 由 Spring 依赖注入来注入-->
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/> <!-- xxxAction 使用 struts 配置 -->
<package name="default" namespace="/" extends="struts-default">
<!--<!– 配置拦截器 , 要在 action 配置 后才生效–>-->
<!--<interceptors>-->
<!--<!– 这种 方式 在 action 中需要 配置 defaultStack –>-->
<!--<!–<interceptor name="HelloInterceptor" class="Interceptor.HelloInterceptor"/>–>-->
<!--<!– 这种方式 只需要在 action 中 配置 HelloStack –>-->
<!--<interceptor name="HelloInterceptor" class="Interceptor.HelloInterceptor"/>-->
<!--<interceptor-stack name="HelloStack">-->
<!--<interceptor-ref name="defaultStack"/>-->
<!--<interceptor-ref name="HelloInterceptor"/>-->
<!--</interceptor-stack>-->
<!--</interceptors>--> <!-- 全局结果集 -->
<global-results>
<result name="error">/WEB-INF/Exception/MyException.jsp</result>
<result name="exception">/WEB-INF/Exception/MyException.jsp</result>
</global-results> <!-- 异常抛出 配置-->
<global-exception-mappings>
<exception-mapping exception="Exception.MyException" result="exception"></exception-mapping>
</global-exception-mappings> <!-- 基于 通配符 的 方式 , 由于整合了Spring ,class = "应该是Spring所注入的对象"-->
<action name="*_*" class="{1}Action" method="{2}">
<!-- 拦截器 生效 ,需要有继承 defaultStack -->
<!--<interceptor-ref name="HelloInterceptor"/>-->
<!--<interceptor-ref name="defaultStack"/>-->
<!--<interceptor-ref name="HelloStack"/>-->
<result name="success">/WEB-INF/jsp/{1}/{2}.jsp</result>
<!-- 重定向 type = "redirect" -->
<result type="redirect" name="redirect">/${url}</result>
</action> </package> </struts>

  3、web.xml (创建 Spring 的 监听器,Struts2 的 过滤器)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <!-- 创建Spring 的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 的监听器通过这个上下文参数 获取 beans.xml 位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:beans.xml</param-value>
</context-param> <!-- OpenSessionInViewFilter 过滤器 (连接 表现层与Dao层 数据交互)-->
<filter>
<filter-name>openSessionInViewerFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- Struts 过滤器 -->
<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> </web-app>

三、总结:

  赶时间做出来的,还没写完整(目前整合了hibernate4 与 Spring ,快断网了。。),如果有错的地方请大家指出,方便大家一起学习。

  SSH 框架整合 完成了,有点激动。。

  这只是基础的SSH整合,接下来我会分享具体的项目设计,希望大家一起学习,方便指出不足之处。

  传送门 :

 github:https://github.com/ShunC/Invest

  

Struts2,Spring3,Hibernate4整合--SSH框架的更多相关文章

  1. 【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD_1.2

    作者: hzboy192@192.com Blog: http://my.csdn.net/peng_hao1988 版本总览:http://blog.csdn.net/peng_hao1988/ar ...

  2. 【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD_1.3

    作者: hzboy192@192.com Blog: http://my.csdn.net/peng_hao1988 版本总览:http://blog.csdn.net/peng_hao1988/ar ...

  3. 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境

    做了三年多的JavaEE开发了,在平时的JavaEE开发中,为了能够用最快的速度开发项目,一般都会选择使用Struts2,SpringMVC,Spring,Hibernate,MyBatis这些开源框 ...

  4. Maven搭建Struts2+Spring3+Hibernate4框架

    做了三年多的JavaEE开发了,在平时的JavaEE开发中,为了能够用最快的速度开发项目,一般都会选择使用Struts2,SpringMVC,Spring,Hibernate,MyBatis这些开源框 ...

  5. Maven02——回顾、整合ssh框架、分模块开发、私服

    1 回顾 1.1 Maven的好处 节省空间 对jar包做了统一管理 依赖管理 一键构建 可跨平台 应用在大型项目可提高开发效率 1.2 Maven安装部署配置 1.3 Maven的仓库 本地仓库 远 ...

  6. Maven项目整合SSH框架

    ---------------------siwuxie095                                         Maven 项目整合 SSH 框架         创建 ...

  7. maven学习记录三——maven整合ssh框架

    6       整合ssh框架 6.1     依赖传递 只添加了一个struts2-core依赖,发现项目中出现了很多jar, 这种情况 叫 依赖传递 6.2     依赖版本冲突的解决 1.  第 ...

  8. Maven 整合SSH框架

    1. 传递依赖冲突 1.1 传递依赖:A(项目)依赖B,B依赖C(1.1版本),B是A的直接依赖,C是A的传递依赖; A(项目)又依赖D,D依赖C(1.2版本),此时,C有两个版本,产生冲突; 1.2 ...

  9. Maven 整合 SSH 框架

    前面的一系列文章中,我们总结了三大框架:Struts2,Hibernate,Spring 的基本知识.本篇就姑且尝试着使用 Maven 这个项目构建工具来将这三个框架整合一起.说到这里,如果有对 Ma ...

随机推荐

  1. VirtualBox只能生成32位虚拟机

    /************************************************************************* * VirtualBox只能生成32位虚拟机 * ...

  2. wpf 客户端【JDAgent桌面助手】开发详解(一)主窗口 圆形菜单

    目录区域: wpf 客户端[JDAgent桌面助手]业余开发的终于完工了..晒晒截图wpf 客户端[JDAgent桌面助手]开发详解-开篇 内容区域: 这里开始主窗口 圆形菜单制作的过程,首先请大家看 ...

  3. sourcetree回退到历史节点

    1. 原理 原理,我们都知道Git是基于Git树进行管理的,要想要回滚必须做到如下2点: 本地头节点与远端头节点一样(Git提交代码的前提条件):于本地头节点获取某次历史节点的更改.说的有点抽象,以图 ...

  4. java内存上堆和栈的一些理解

    多线程上的基本类型:https://blog.csdn.net/championhengyi/article/details/76857401

  5. SpringCloud初体验:三、Feign 服务间调用(FeignClient)、负载均衡(Ribbon)、容错/降级处理(Hystrix)

    FeignOpenFeign Feign是一种声明式.模板化的HTTP客户端. 看了解释过后,可以理解为他是一种 客户端 配置实现的策略,它实现 服务间调用(FeignClient).负载均衡(Rib ...

  6. 建立SSH隧道从外网访问内网服务器

    http://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examp ...

  7. coding github 配置ssl 免密拉取代码

    详细介绍: https://www.cnblogs.com/superGG1990/p/6844952.html 注:其中检验过程与下述不同,可以先在对应git库使用 git pull 一次,选择信任 ...

  8. php 的两个扩展 memcache 和 memcachd

    今天绕了很大弯子, 因为自己写的php的memcache类. 于是出现了下面问题 在本地测试好好的, 线上就出了问题 原因是线上使用的是memcache, 我本地使用的是memcached 区别参考网 ...

  9. mariadb master and salve configure

    mariadb master and salve configure 主从复制配置: master:192.168.8.200 salve:192.168.8.201 主服务器配置: 主服务器需要启动 ...

  10. Fatal error: Call to undefined function Think\C() in /var/www/html/ceshi.hzheee.com/think/ThinkPHP/Library/Think/Think.class.php on line 334 这个问题解决

    当APP_DEBUG为true时,包含图中这个文件,文件中又引导包含这些库文件,可以看出安装thinkphp3.2.3时ThinkPHP/Common/下是functions.php,把它改成func ...