您的位置:首页 > 其它

ssh2 三大框架整合

2014-06-16 14:25 295 查看
提示:eclipse环境、工程环境、tomcat环境的jdk保持一致

1、新建一个工程,把工程的编码为utf-8

2、把jsp的编码形式改成utf-8

3、把jar包放入到lib下 (eclipse下jar包要放在lib下,不能在lib下还有文件夹)

4、建立三个src folder

src 存放源代码

config 存放配置文件

hibernate 存放hibernate的配置文件

spring 存放spring的配置文件

struts 存放struts的配置文件

struts.xml

test 存放单元测试

5、在src下建立包

cn.itcast.s2sh.domain

持久化类和映射文件

6、编写dao层和service层

7、写spring的配置文件

1、写sessionFactory

2、测试

3、写dao和service

4、测试

8、写action

9、写spring的配置文件

把action注入到spring容器中

<bean id="personAction" class="cn.itcast.s2sh.struts2.action.sh.PersonAction" scope="prototype">

scope为"prototype"保证了action的多实例

10、在web.xml

加入spring的监听器

加入struts2的过滤器

11、请求

三大框架整合原理

1、三大框架的作用

struts2是一个mvc框架

spring容器

1、利用ioc和di做到了完全的面向接口编程

2、由于spring的声明式事务处理,使程序员不再关注事务

3、dao层和service层的类是单例的,但是action层是多例

hibernate

就是一个数据库的ormapping的框架

2、整合原理

1、当tomcat启动时,做的事情

1、因为在web.xml中,

<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>

<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>

所以在启动的时候,执行的是

ContextLoaderListener

contextInitialized

this.contextLoader = createContextLoader();

加载spring的配置文件

这里有一个固定的参数con的textConfigLocation

可以指定classpath路径下的spring的配置文件

也可以任意位置指定配置文件 spring*.xml WEB-INF/任意多个任意文件夹/spring-*.xml

如果没有指定固定参数,则查找默认的加载路径:WEB-INF/applicationContext.xml

this.contextLoader.initWebApplicationContext(event.getServletContext());

启动spring容器

总结:当tomcat启动的时候,spring容器就启动了,这个时候service层和dao层所有的单例类就创建对象了

struts2容器:

加载了default.properties,struts-default.xml,struts-plugin.xml,struts.xml

2、请求一个url时,发生的事情:

1、在引入jar包时,导入了struts2-spring-plugin-2.1.8.1.jar包,该jar中有一个文件struts-plugin.xml

<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring"

class="org.apache.struts2.spring.StrutsSpringObjectFactory" />

<constant name="struts.objectFactory" value="spring" />

2、由于上面的配置改变了action的生成方式,action由StrutsSpringObjectFactory生成,经过查找是由SpringObjectFactory中的buidBean方法

生成的

try {

o = appContext.getBean(beanName);

} catch (NoSuchBeanDefinitionException e) {

Class beanClazz = getClassInstance(beanName);

o = buildBean(beanClazz, extraContext);

}

3、由上面的代码可以看出,先从spring容器中查找相应的action,如果没有找到,再根据反射机制创建action,

beanName就是struts配置文件class属性的值,所以class属性的值和spring中ID的值保持一致
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: