zoukankan      html  css  js  c++  java
  • DispatcherServlet和ContextLoaderListener是什么?

    20180509 check

    接收到HTTP请求后,DispatcherServlet 询问HandlerMapping (configuration files) 去请求合适的Controller。Controller接受请求后,调用合适的service方法,然后set model data,接下来返回视图(view)给DispatcherServlet。DispatcherServlet从ViewResolver 选择定义好的视图给request请求。一旦视图确定下来,DispatcherServlet 传递model data给最终渲染到浏览器上的视图。

    <web-app>
      <display-name>Archetype Created Web Application</display-name>
       
      <servlet>
            <servlet-name>spring</servlet-name>
                <servlet-class>
                    org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
     
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
         
    </web-app>

    ContextLoaderListener 读取spring配置文件(“contextConfigLocation” in web.xml),解析它、加载定义在配置文件中的beans。

    <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    WEB-INF/spring-dao-hibernate.xml,
                    WEB-INF/spring-services.xml,
                    WEB-INF/spring-security.xml
                </param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
     
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

     注:也可以将多个context files放到一个已经存在的配置文件中

    <beans>
        <import resource="spring-dao-hibernate.xml"/>
        <import resource="spring-services.xml"/>
        <import resource="spring-security.xml"/>
         
        ... //Other configuration stuff
     
    </beans>

    <context:annotation-config> = Scanning and activating annotations in “already registered beans”.

    <context:component-scan> = Bean Registration + Scanning and activating annotations

  • 相关阅读:
    slim中返回结果加密的
    windows下的redis和redismyadmin
    centos6.2升级到centos6.8(6.5应该也一样)
    剑指offer-二叉树的深度
    leetcode-【hard】4. Median of Two Sorted Arrays
    leetcode-【中等题】5. Longest Palindromic Substring
    leetcode-【中等题】3. Longest Substring Without Repeating Characters
    leetcode-【中等题】2. Add Two Numbers
    leetcode-【中等题】228. Summary Ranges
    leetcode-【hard】273. Integer to English Words
  • 原文地址:https://www.cnblogs.com/skyball/p/9012146.html
Copyright © 2011-2022 走看看