//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"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-3.0.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-3.0.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-3.0.xsd">
2.3在web.xml中添加spring监听:
contextConfigLocation
/WEB-INF/classes/applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
配置监听的目的:
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。
3添加hibernate
3.1数据库连接驱动
如果没有需要的数据库连接驱动,需要在添加特性之前,添加数据库连接驱动
Window->openperspective->myeclipsedatabaseexplorer
目前没有我们需要的驱动,我们这里使用mysql。
下一步
点击finish.
创建完毕之后,打开连接
这样,在添加hibernate特性时就会出现数据库连接驱动选择项。
3.2添加hibernate特性
同样,也不添加library
选择springconfigurationfile,点击下一步
选择已经存在的spring配置文件,点击下一步。
在这个对话框,dbdriver中选择刚才已创建的myconn。
这里不用再拷贝驱动到buildpath。
因为已经把相关的jar包拷贝过去了。
下一步。
不用创建hibernatesessionfactory,点击finish。
3.3查看applicationContext.xml配置文件
增加的内容:
class="mons.dbcp.BasicDataSource">
value="com.mysql.jdbc.Driver">
mysql:
//localhost:
3306/hpdb">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
org.hibernate.dialect.MySQLDialect
这里既是applicationContext.xml中会自动把数据源dataSource,sessionFactory添加。
3.4反向映射
3.4.1创建pojp和hbm文件所在的包。
3.4.2打开databaseexplorer
3.4.3打开数据库连接
3.4.4选择要反向映射的表,右键选择hibernate反向工程
第一个选项是创建hbm.xml文件,必须的。
第二个选项是创建pojo,必须的,创建pojo,事先需要创建包,com.hp.po。
第三个选项是创建dao。
这里因为我们要自己做dao,因此不用创建dao了。
下一步选择生成策略,native。
identity
这里如果不修改类名,会根据相关的策略,生成类名。
例如TUser。
3.4.5生成后问题及解决方式
生成后:
。
方法:
在生成类的时候指定包名。
如果移动路径,需要修改内容:
applicationContext.xml:
./User.hbm.xml
com/hp/po/Log.hbm.xml
修改为:
com/hp/po/User.hbm.xml
com/hp/po/Log.hbm.xml
User.hbm.xml:
修改为:
hibernate.reveng.xml
hibernate.reveng.xml文件可以删掉了
4添加struts
4.1添加struts特性
因为已经添加了struts2的jar包,因此不用在添加。
点击finish。
完成后主要做了两个工作。
一个是生成struts.xml,空文件,没有实质内容
在web.xml中添加struts的过滤器:
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
FilterDispatcher是struts2.0.x到2.1.2版本的核心过滤器。
StrutsPrepareAndExecuteFilter是自2.1.3开始就替代了FilterDispatcher的。
4.2纳入spring容器管理
--纳入spring容器管理-->
整合的时候需要此操作需要struts2-spring-plugin-xxx.jar
5常用配置添加
5.1在applicationContext.xml文件中
5.1.1常用配置
在中添加
--设置Hibernate方言-->
org.hibernate.dialect.MySQLDialect
--是否打印sql-->
true
--格式化sql-->
true
--是否自动更新表-->
update
thread
--最大抓取深度,如果为0,则关闭默认的外连接抓取。
建议值为0-3-->
1
--用于生成有助于调试的注释信息,默认为关闭-->
true
5.1.2事务配置
--声明式事务配置-->
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
--AOP切面声明事务管理-->
adviceid="txAdvice"transaction-manager="transactionManager">
attributes>
--支持当前事务,如果执行到save开头的任何方法时没有事务则开启一个事务-->
methodname="save*"propagation="REQUIRED"/>
--支持当前事务,如果执行到update开头的任何方法时没有事务则开启一个事务-->
methodname="update*"propagation="REQUIRED"/>
--支持当前事务,如果执行到add开头的任何方法时没有事务则开启一个事务-->
methodname="add*"propagation="REQUIRED"/>
--支持当前事务,如果执行到delete开头的任何方法时没有事务则开启一个事务-->
methodname="delete*"propagation="REQUIRED"/>
--支持当前事务,查询没有事务,就以非事务方式执行。
只读-->
methodname="get*"propagation="SUPPORTS"read-only="true"/>
--支持当前事务,查询没有事务,就以非事务方式执行。
只读-->
methodname="find*"propagation="SUPPORTS"read-only="true"/>
methodname="*"read-only="true"propagation="NOT_SUPPORTED"/>
attributes>
advice>
在struts.xml文件中添加:
--开发常量配置-->
--指定Struts2是否为开发模式,若为开发模式,程序运行错误时会得到更多的信息-->
--设置浏览器是否缓存静态页面,开发阶段:
false,上线:
true-->
--当struts.xml文件改动时,是否重新加载该文件,为提高开发效率,开发阶段是true-->
--指定默认编码规则,对于请求参数中含有中文的时候应该使用UTF-8-->
--默认的视图-->
--指定全局的国际化标签-->
--上传下载-->
--指定请求的后缀名,默认为action,若为多个的话,之间用逗号隔开-->
--将struts2的action交给Spring管理-->
--引入各模块业务的struts配置
-->