SSH三大框架整合步骤:
1.新建web工程,并将其编码设置utf-8
2.把整个jsp页面改成utf-8编码格式
3.建立三个source folder文件
src ----存放源代码
config----存放所有的配置文件,即struts,hibernate和spring配置文件
test----存放测试类
4.在WebRoot\WEB-INF\lib导入相应的jar包
整合原理:
1. 配置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>
说明:
createContextLoader(),加载springweb容器
initWebApplicationContext:
* 初始化spring的web容器
* 加载其配置文件
总结:
当执行完这两个方法以后,就启动spring的web容器了,在spring容器中,单例模式的bean就被实例化了,所以dao和service层的对象和代理对象就在这产生了。
- 以过滤器的形式整合struts2
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
tomcat服务器启动:一是加载各种配置文件:default.properties、struts-default.xml、struts-plugin.xml、struts.xml ;二是静态注入一些bean
2.请求url时 ,步骤如下:
* 先找struts的配置文件,根据struts2的相关配置查找action的创建方式
* 在uts-default.xml,struts-plugin.xml,struts.xml配置文件中查找struts.objectFactory变量由哪个类创建了action,哪个配置文件加载再最后,哪个决定
* 最后在struts和spring整合的包中找到了struts-plugin.xml文件
<beantype="com.opensymphony.xwork2.ObjectFactory"
name="spring"class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constantname="struts.objectFactory" value="spring" />
总结:由上述的内容可以知道,action是由StrutsSpringObjectFactory创建,而该类继承了SpringObjectFactory,并且执行父类的buildBean来创建action
<pre name="code" class="java"> public Object buildBean (String beanName,Map<String,Object>extraContext,boolean injectInternal) {
object o = null;
try {
o = appContext.getBean(beanName);
} catch (NoSuchBeanDefinitionException e) {
Class beanClazz = getClassInstance(beanName);
o = buildBean(beanClazz, extraContext);
}
if ( injectInternal ){
injectInternalBeans(0);
}
return o;
}
总结:
由上面的代码可以看出,先从spring容器中查找相应的action,如果没有找到,再根据反射机制创建action,beanName就是struts2配置文件中的action元素的class属性的值,这就意味着class属性的值要和spring容器中action所在的bean所指定的id值要一致。