• ssh2 三大框架整合


    提示: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的值保持一致

  • 相关阅读:
    C#数据类型
    面试常备题素数的判断
    Eclipse输入命令行参数
    Socket编程第一步PC上服务器回显客户端发送的字符串
    C# 格式化输出
    截止至2004年8月1日全球CCIE人数统计
    什么是3G?
    DoS 拒绝服务攻击专题
    我的Blog音乐版~~
    国庆啊~~累得我好苦啊~~~
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3790889.html
Copyright © 2020-2023  润新知