Spring+Hibernate+Struts2整合[web.xml配置][2/6]

上一篇文章,主要讲解了整合的整体思路,这一章节主要讲解一下对于web.xml的配置


 

web.xml主要的、关键的内容有2方面:

  1. 1.Spring
      •    监听器
  1. ContextLoaderListener:Spring配置文件applicationContext.xml在学习时,通过classpathxml...这个类去加载的,在web中通过这个监听器去读取创建Spring容器,当web启动时,Spring跟着被加载。
  2. IntrospectorCleanupListener:此监听器主要用于解决java.beans.Introspector导致的内存泄漏的问题;通俗易懂的理解就是,定期清理一些没有用的垃圾对象,提高应用性能。
  <!-- ======================== Introduction ============================== -->
  <!--                                                                      -->
  <!--                            Spring 监听器                                                                                             -->
  <!--                                                                      -->
  <!--                   1.   ContextLoaderListener                         -->
  <!--                   2. IntrospectorCleanupListener                     -->
  <!--                                                                      -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
      • 过滤器

OpenSessionInViewFilter:这个过滤器是用来解决hibernate的延迟加载问题,涉及到懒加载,比如Load方法,大概意思当使用时才会去数据库去查询,导致有一种可能就是去数据库查询时,session已经关闭了,连接都断开了肯定就不会查询到了。这个监听器就是在一个请求到达action之前开启session,当请求结束了,再关闭session,就解决了no-session的问题了。

  <!-- ======================== Introduction ============================== -->
  <!--                                                                      -->
  <!--                            Spring 过滤器                                                                                             -->
  <!--                                                                      -->
  <!--                       OpenSessionInViewFilter                        -->
  <!--                                                                      -->
    <filter>
        <filter-name>openSession</filter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBean</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>flushMode</param-name>
            <param-value>AUTO</param-value>
        </init-param>
    </filter>
  1. 2.Struts2

过滤器

StrutsPrepareAndExecuteFilter:Struts2的启动入口!

  <!-- ======================== Introduction ============================== -->
  <!--                                                                      -->
  <!--                          struts2 过滤器                                                                                                -->
  <!--                                                                      -->
  <!--                  StrutsPrepareAndExecuteFilter                       -->
  <!--                                                                      -->    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>openSession</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 

以上就是对web.xml的大致内容,接下来将会对POJO类、service、dao、action类进行详细的讲解,详情请看下一章节的内容!

posted @ 2018-06-15 10:09  呦,可以呦  阅读(101)  评论(0)    收藏  举报