从0开始学习ssh之搭建环境
ssh即struts+spring+Hibernate,从头开始学习这个框架。
struts环境配置,首先在apps目录下找到struts2-blank-xxx.war这个文件,这是已经发布好的war包。其中找到lib文件夹,添加其中所有jar到网站lib下面。再找到src/java下面的struts.xml文件,添加到网站的src文件夹中。打开web.xml拷贝struts的核心配置到网站的web.xml,核心配置如下
<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>
之后,修改struts.xml
<struts>
<!-- 开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- 扩展名配成action -->
<constant name="struts.action.extension" value="action" />
<!-- 主题配置为simple -->
<constant name="struts.ui.theme" value="simple" /> <package name="default" namespace="/" extends="struts-default">
</package> <!-- Add packages here --> </struts>
配置了开发模式,扩展名为action,以及模板为simple
以上就是struts2的配置,之后自己写aciton部分就可以
hibernate配置
添加和ibernate3.jar lib文件夹下required的所有,jpa下的所有,optional下c3p0的所有(数据库连接池),mysql-connector-java-5.1.5-bin,然后把log4j.properties(日志文件)、hibernate.cfg.xml(配置文件),放到网站src目录下.
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> <!-- 1,数据库连接信息 -->
<property name="dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</property>
<!--
<property name="connection.url">jdbc:mysql:///数据库名</property>
<property name="connection.driver_class">com.jdbc.mysql.Driver</property>
<property name="connection.username">数据库用户名</property>
<property name="connection.password">数据库密码</property>
--> <!-- 2,其他配置 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property> <!-- 3,导入映射文件 -->
<mapping resource="cn/itcast/oa/domain/User.hbm.xml" /> </session-factory> </hibernate-configuration>
以上是hibernate的配置
spring配置
添加spring.jar,aspectj文件夹下所有,cglib-nodep-2.1_3.jar,commons-logging.jar(日志),
applicationContext.xml放在src下,代码如下
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 自动扫描与装配bean -->
<context:component-scan base-package="cn.itcast.oa"></context:component-scan> </beans>
以上是spring配置
总结一下
Struts2
jar包
struts.xml, web.xml
Hibernate
jar包:核心包, 必须包, jpa, c3p0, jdbc
hibernate.cfg.xml
Spring
jar包
appicationContext.xml
接下来就是要进行整合
首先整合spring和struts2
1.在web.xml中配置Spring的监听器
<!-- 配置Spring的用于初始化容器对象的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
2.lib中添加struts2-spring-plugin-2.1.8.1 jar包
这样整合之后struts.xml可以直接写bean的名称
接下来进行Hibernate与Spring整合
1.管理SessionFactory实例(只需要一个)
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 指定hibernate的配置文件位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置c3p0数据库连接池 -->
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
</property>
</bean>
2,声明式事务管理
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
从0开始学习ssh之搭建环境的更多相关文章
- pyqt4学习之一:搭建环境和入门
还在继续写Python小工具,想起之前用Tkinter被坑得半死,决定换个框架写UI,又想顺便了解一下qt,就学习一下pyqt4 搭建环境 win:现在安装包 http://www.riverbank ...
- AspectJ基础学习之二搭建环境(转载)
AspectJ基础学习之二搭建环境(转载) 一.下载Aspectj以及AJDT 上一章已经列出了他的官方网站,自己上去download吧.AJDT是一个eclipse插件,开发aspectj必装,他可 ...
- django 学习第一天搭建环境
目前django版本是1.10,我学习的基础教材是 Web Development with Django Cookbook, Second Edition 搭建好配置环境 ssh免认证登录 修改一下 ...
- JAVAEE——BOS物流项目01:学习计划、搭建环境、主页设计(jQuery EasyUI)
1 学习计划 1.项目概述 项目背景介绍 2.搭建项目开发环境 数据库环境 maven项目搭建 svn环境搭建 3.主页设计(jQuery EasyUI) layout页面布局 accordion折叠 ...
- struts2学习笔记--动手搭建环境+第一个helloworld项目
在Myeclipse中已经内置好了struts2的环境,但是为了更好的理解,这里自己从头搭建一下: 前期准备:下载struts2的完整包,下载地址:https://struts.apache.org/ ...
- spring boot学习01【搭建环境、创建第一个spring boot项目】
1.给eclipse安装spring boot插件 Eclipse中安装Spring工具套件(STS): Help -> Eclipse Marketplace... 在Search标签或者Po ...
- Nginx学习系列一搭建环境
1.Win10下安装vmware14虚拟机软件 官方下载地址 全程next,输入key,激活即可. 2.在虚拟机中安装Linux服务器环境,操作系统为Centos7 继续下一步,安装完成! 3.下载C ...
- 从0开始学习ssh之basedao
用于所有dao里边会有许多相同的方法,例如save,update等等.应此设计一个basedao,所有dao都继承它.这样可以省去许多工作量. basedao如下 package cn.itcast. ...
- 从0开始学习ssh之资源分类
更目录下面,新建config用于放配置文件,新建test用于放置测试文件.src目录用于放置源代码.由于ssh是三层,因此新建三层包(dao,service,view).其中dao和service还有 ...
随机推荐
- 用于扩展目标跟踪的笛卡尔B-Spline车辆模型
(哥廷根大学) 摘要 文章提出了一种表示空间扩展物体轮廓的新方法,该方法适用于采用激光雷达跟踪未知尺寸和方向的车辆.我们在笛卡尔坐标系中使用二次均匀周期的B-Splines直接表示目标的星 - 凸形状 ...
- window 系统上传文件到linux 系统出现dos 格式换行符
Windows里的文件在Unix/Mac下打开的话,在每行的结尾可能会多出一个^M符号,Unix/Mac系统下的文件在Windows里打开的话,所有文字会变成一行,所以为了避免这种情况的发生,我们可以 ...
- [JZOJ3337] 【NOI2013模拟】wyl8899的TLE
题目 题目大意 给你两个字符串\(A\)和\(B\),可以修改\(A\)中的一个字符,求修改后最长的\(A\)的前缀,使它是\(B\)的子串. 思考历程 看到这道题之后,第一眼想到的就是后缀自动机! ...
- QQ空间删除日志
按下F12,贴上如下代码 var delay = 2000; function del() { document.querySelector(".app_canvas_frame" ...
- Linux课程---15、域名相关
Linux课程---15.域名相关 一.总结 一句话总结: 先购买域名,再备案,再解析,域名即可使用 1.域名备案是怎么回事(比如二级域名,三级域名)? 每个二级域名需要备案一次,三级域名不需要备案, ...
- vue之.native修饰符
.native 修饰符就是用来注册元素的原生事件而不是组件自定义事件的 比如:自定义 Button.vue 组件 <template> <button type="butt ...
- iconfont 在vue项目中的应用(icon-component组件)
前言:上一篇记录了iconfont的三种基本使用方法. 在Vue中应该如何使用呐?Vue中的组件化思想很明确,要提高组件的复用率,增强项目的可维护性,扩展性.以下是采用icontfont的使用方式之s ...
- Java学习之创建对象内存使用机制
Java内存空间分两种,一种是栈内存,有多个,一种是堆内存,只有一个,在堆内存中又有一块方法区. 方法区中存储的是:类的信息(类名,类的直接父类,类的访问修饰符),类变量,类方法代码,实例方法代码,常 ...
- assert(断言)
Python assert(断言)用于判断一个表达式,在表达式条件为 false 的时候触发异常. 语法格式: assert expression 等价于: if not expression: ra ...
- Asp.Net中的HttpWebRequest类与HttpWebResponse类
相关博文:https://www.cnblogs.com/xu-yi/p/10061342.html 相关博文:https://www.cnblogs.com/zoujinhua/p/11313396 ...