SSH学习系列篇——整合步骤+原理

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值要一致。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值