SSH框架整合开发具体解释(个人笔记)
一.创建数据库并设置编码。
A) create database oa default character set utf8。
二.MyEclipseproject
A) 在Myeclipse里创建webproject,并设置编码为utf8.
B) 加入框架环境
1.加入Junit4 library(Myeclipse自带)
2.加入Struts2环境
①所需Jar包
②配置文件:拷贝一个struts.xml模版到src文件夹,进行适当改动,在web.xml里配上须要的代码。
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <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">
<action name="" class="">
<result name=""></result>
</action>
</package>
<!-- Add packages here --> </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>3.加入Hibernate环境
①所需Jar包
②配置文件:拷贝一个hibernate.cfg.xml和映射文件*.hbm.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:///oa</property>
<property name="connection.driver_class">com.jdbc.mysql.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property> <!-- 2,其它配置 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property> <!-- 3。导入映射文件 -->
<mapping resource="" /> </session-factory> </hibernate-configuration>User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.grace.oa.domain"> <class name="User" table="grace_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name" />
</class> </hibernate-mapping>4.加入Spring环境
①所需Jar包
②配置文件:拷贝一个appicationContext.xml/beans.xml模版到src文件夹
appicationContext.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: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> <!-- 导入外部的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置SessionFactory IOC-->
<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> <!-- 配置声明式事务管理(採用注解的方式) AOP-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 注解驱动-->
<tx:annotation-driven transaction-manager="txManager"/> </beans>5.整合Spring和Struts2 (IOC:让struts2的action交由容器管理)
在src文件夹下建Junit測试包进行測试
①单独測试struts2,新建TestAction类
a) TestAction.java
public class TestAction extends ActionSupport{ @Override
public String execute() throws Exception{
System.out.println("===>TestAction.execute()");
return "success";
}
}b)在struts.xml里配置对应action
<!-- 配置測试用的Action,未与Spring整合,class属性写类的全名 -->
<action name="test" class="cn.grace.oa.test.TestAction">
<result name="success">/test.jsp</result>
</action>c)部署測试能否够通过test.action正常訪问test.jsp页面和是否输出"===>TestAction.execute()"
②单独測试Spring,新建SpringTest类(此时appicationContext.xml里面仅仅须要一个装配和扫描bean的语句,否则可能报错)
a) SpringTest.java
public class SpringTest {
private ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); @Test
public void testBean() throws Exception{
TestAction testAction=(TestAction) ac.getBean("testAction");
System.out.println(testAction);
}
}b)此时Junit測试不能成功。必须在TestAction类上进行声明,并配置scope。
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport{ @Override
public String execute() throws Exception{
System.out.println("==>TestAction.execute");
return "success";
}
}c)此时再进行Junit測试成功输出testAction.
d)声明一个bean(注解方式,有四种方式,依据不同类用相相应的方式)
效果跟在bean.xml里面写<bean id="" class=""></bean>功能是一样的
@Component("beanName")//不写beanName默认使用类名首字母小写,即testAction
@Controller
@Service
@Repository
配置bean的scope。默觉得单例,改动为prototype
@Scope("prototype")
③測试Struts2和Spring整合
a)在web.xml中配置一个监听器
<!-- 配置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>b) 加一个jar包:struts2-spring-plugin-2.1.8.1.jar
将struts.xml中的action的class从路径改为bean名称,再进行測试。看是否整合成功
<!-- 配置測试用的Action,未与Spring整合,class属性写类的全名 -->
<!-- 当Struts2与Spring整合后,class属性能够写bean的名称 -->
<action name="test" class="testAction">
<result name="success">/test.jsp</result>
</action>c)部署測试能否够通过test.action正常訪问test.jsp页面,正常訪问test.jsp代表整合成功。
6.整合Spring和Hibernate(1.管理SessionFactory实例(仅仅须要一个)2.声明式事务管理)
①SessionFactory
a)在applicationContext.xml里配置sessionFactoryv(见上文appicationContext.xml),并在hibernate.cfg.xml去掉反复的连接数据库的信息
b)新建一个jdbc.properties用来存放dataSource用到的数据库连接信息。并在applicationContext.xml导入该文件
applicationContext.xml(上文appicationContext.xml已配置)
<!-- 导入外部的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>jdbc.properties
jdbcUrl = jdbc:mysql:///oa?characterEncoding=utf8
driverClass = com.mysql.jdbc.Driver
user = root
password = rootc)在SpringTest类里写測试方法。进行Junit測试。
@Test
public void testSessionFactory() throws Exception{
SessionFactory sessionFactory=(SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}②声明式事务管理
a)在applicationContext.xml里配置声明式事务管理(上文appicationContext.xml已配置)
<!-- 配置声明式事务管理(採用注解的方式) AOP-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 注解驱动-->
<tx:annotation-driven transaction-manager="txManager"/>b)測试声明式事务管理(測试回滚)
i.新建一个用于測试的实体类:User.java(仅仅需定义id和name及其构造方法就可以)
ii.依据User写相应的User.hbm.xml映射文件
iii.在hibernate.cfg.xml导入映射文件
<mapping resource="cn/grace/oa/domain/User.hbm.xml" />iV.新建一个TestService类
@Service("testService")
public class TestService { @Resource
private SessionFactory sessionFactory; @Transactional
public void saveTwoUsers(){
Session session=sessionFactory.getCurrentSession();
session.save(new User());
int a=1/0;//会抛异常,由于声明了事务,因此会回滚
session.save(new User());
}
}V.在SpringTest类里写測试方法,进行Junit測试。
@Test
public void testTransaction() throws Exception{
TestService testService=(TestService)ac.getBean("testService");
testService.saveTwoUsers();
}Vi假设数据库中有新创建的表,而且没有插入数据,则去掉异常int a=1/0;再进行正常插入,假设此时id 从2開始,则 測试成功。由于id为1的user在之前一次測试中。抛异常被回滚。
至此,三大框架整合完成。
7.对三大框架整合进行完整測试。
①改动TestAction.java为例如以下代码
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport{ @Resource
private TestService testService;
@Override
public String execute() throws Exception{
//System.out.println("test");
testService.saveTwoUsers();
return "success";
}
}②Spring管理对象(事务),action处理请求。hibernate处理对象的存储。
③假设此时通过test.action能正常显示test.jsp而且在数据库中的user表中添加了2条记录,则整合成功。(能够再添加异常进行重复測试,假设还出现回滚现象,则代表成功)
SSH框架整合开发具体解释(个人笔记)的更多相关文章
- dwr与ssh框架整合教程
(1)dwr与ssh框架整合教程dwr框架介绍. DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开 发人员开发包含AJ ...
- ssh框架整合之登录以及增删改查
1.首先阐述一下我用得开发工具,myeclipse2017+oracle,所以我的基本配置步骤可能不一样,下面我用几张图来详解我的开发步骤. ---1先配置structs (Target 选择apac ...
- Spring+Hibernate+Struts(SSH)框架整合
SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...
- SSH框架整合过程总结
---------------------siwuxie095 SSH 框架整合过程总结 (一)导入相关 jar 包(共 ...
- SSH框架整合
SSH框架整合 一.原理图 action:(struts2) 1.获取表单的数据 2.表单的验证,例如非空验证,email验证等 3.调用service,并把数据传递给service Service: ...
- J2EE进阶(十)SSH框架整合常见问题汇总(一)
SSH框架整合常见问题汇总(一) 前言 以下所列问题具有针对性,但是遇到同类型问题时均可按照此思路进行解决. HTTP Status 404 - No result defined for actio ...
- SSH框架项目开发命名规范
SSH 框架项目开发命名规范 一.各层包及类命名规范 总体原则:包名所有字母小写,类名采用 "驼峰标识",具体如下: 1. Action 类 包命名规范:co ...
- 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 ...
- SSH框架整合的其它方式
--------------------siwuxie095 SSH 框架整合的其它方式 1.主要是整合 Spring 框架和 Hibernate 框架时,可以不写 Hibernate 核心配置文件: ...
随机推荐
- python之函数用法isupper()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法isupper() #http://www.runoob.com/python/att ...
- html 5检查Mobile App是否在线
在PhoneGap应用,或者黑莓Java/HTML混搭应用中,如何使用JavaScript检查Mobile App网络连接状态呢? <script type="text/javascr ...
- docker overlay存储驱动介绍(传送门)
https://blog.csdn.net/u010278923/article/details/79215828
- [Dubbo实战]dubbo + zookeeper + spring 实战 (转)
这里最熟悉的就是spring了,项目中应用很多.dubbo是一个实现分布式的框架,zookeeper是注册中心.给我的感觉就像多对多关系的两者表,zookeeper相当于第三张表维护关系.下面通过一个 ...
- rhel7.x配置本地yum
转载:http://www.mvpbang.com/articles/2017/12/22/1513948827684.html rhel7.x配置本地yum 环境: centos7.4 vmarew ...
- poj 2059 单调栈
题意:求柱状图中最大矩形面积. 单调栈:顾名思义就是栈内元素单调递增的栈. 每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素.直到满足当前须要插入的元素大于栈顶元 ...
- MyChatRoom——C#自制聊天室
一个用C#编写的基于Socket的Windows版聊天室,包括服务端和客户端.当服务端启动服务后,客户端可以连接到服务端,给服务端发送数据,服务端可以接收数据:服务端可以给客户端发送数据,客户端接收: ...
- 网站PV、UV以及查看方法
网站PV.UV以及查看方法 一.名词解释 PV:PV 是Page Views的缩写,即页面浏览量,用户每一次对网站中的每个网页访问均被记录一次.注意,访客每刷新一次页面,pv就增加一次. UV:UV是 ...
- 听听各位对Ubuntu的UI的看法
2012-7-15 15:46 最近升级到Ubuntu 12.4 .发现其界面效果真的时越来越炫啦.我就在想,你Ubuntu你图什么啊, 你是以个Linux系统,你的重点在于让系统运行更稳定,更 ...
- LFU缓存
https://leetcode-cn.com/problems/lfu-cache/description/ 缓存的实现可以采取多种策略,不同策略优点的评估就是"命中率".好的策 ...