spring+struts2+hibernate调整
三大框架的整合步骤:
* 新建一个工程
* 把整个工程的编码改变成utf-8
* 把整个jsp页面也改变成utf-8
* 导包
web-inf
lib
struts
hibernate
spring
db
junit
* 建三个src folder
src 存放源代码的
cn.itcast.s2sh0909.struts2.action
cn.itcast.s2sh0909.dao
cn.itcast.s2sh0909.dao.impl
cn.itcast.s2sh0909.service
cn.itcast.s2sh0909.service.impl
cn.itcast.s2sh0909.domain
config 存放所有的配置文件
struts2
hibernate
spring
applicationContext.xml
applicationContext-db.xml
...........
test 存放测试类
cn.itcast.s2sh0909.test
* 在dao和service层相应的包中写接口和类
* 在applicationContext-db.xml文件中写sessionFactory
* 在cn.itcast.s2sh0909.test包中新建一个类SessionFactoryTest,目的是为了测试SessionFactory
是否配置正确
* 写spring的声明式事务处理
* 在spring的配置文件中写dao和service
* 通过savePerson方法测试声明式事务处理
* 编写action
* 编写struts2的配置文件
* 编写web.xml文件
* 测试
整合原理:
* web.xml
* spring容器是以监听器的形式与tomcat整合的
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
* 以过滤器的形式整合struts2容器
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
说明:
* createContextLoader() 加载spring的web容器
* initWebApplicationContext
* 初始化spring的web容器
* 加载其配置文件
* 当执行完这两个方法以后,就启动spring的web容器了,在spring容器中,单例模式的
bean就被实例化了,所以dao和service层的对象和代理对象就在这个时候产生了
当tomcat启动的时候
在tomcat启动的时候,干了两件事情:
* 加载了各种配置文件
* 静态注入了一些bean
请求url:personAction_savePerson.action
步骤
* 先找struts的配置文件,会找根据struts2的相关配置查找action的创建方式
* 会去常量struts.objectFactory查找到底是由哪个类创建了action
* 会去struts-default.xml,struts-plugin.xml,struts.xml文件去找struts.objectFactory
* 哪个配置文件加载在最后,哪个决定
* 最后在struts和spring整合的包中找到了struts-plugin.xml文件
<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.objectFactory" value="spring" />
* 由上述的内容可以知道,action是由StrutsSpringObjectFactory创建的,而该类继承了SpringObjectFactory
该源代码中beanName就是struts2配置文件中的action元素的class属性的值,这就意味着
class属性的值要和spring容器中action所在的bean所指定的id值要一致