zoukankan      html  css  js  c++  java
  • web.xml配置DispatcherServlet (***-servlert.xml)

     1. org.springframework.web.servlet.DispatcherServlet

    所在jar包:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>

    默认配置文件:

      DispatcherServlet.properties

    上下文:

      该DispatcherServlet默认使用WebApplicationContext作为上下文。

    2. DispatcherServlet的作用:

      DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,主要负责流程的控制。

      对应的是 controller 级别的配置,作用范围是控制层上下文。

    3. DispatcherServlet怎么配置

      servlet-name :随便取。这里配置成[test]。Spring默认配置文件为"/WEB-INF/[servlet名字]-servlet.xml"。

      load-on-startup : web.xml中可配置多个servlet。 load-on-startup可指定在系统启动时按顺序加载servlet。

      url-pattern :表示哪些请求交给Spring Web MVC处理。此处会处理所有URI为"appName/test/*"的请求。

        <!--Spring view分发器-->
    <servlet> <servlet-name>test</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/test/*</url-pattern> </servlet-mapping>

    4. [servlet名字]-servlet.xml里面的配置

    4.1 mvc:annotation-driven

      <mvc:annotation-driven />

    为什么要配置annotation-driven:

      <mvc:annotation-driven />注册了Spring MVC分发请求到控制器所必须的DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter实例。
    如果没有<mvc:annotation-driven/>,那么所有的Controller可能就没有解析。

    4.2 context:component-scan

        <context:component-scan base-package="com.test.web.controller" />

    context:component-scan又干了啥:

    a). 有了<context:component-scan>,另一个<context:annotation-config/>标签根本可以移除掉,因为已经被包含进去了。
    b). <context:component-scan>提供两个子标签:<context:include-filter>和<context:exclude-filter>各代表引入和排除的过滤。
    c). spring会自动去扫描base-package下面或者子包下面的Java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean。

    4.3 mvc:interceptors

    <mvc:interceptors>
        <bean class="com.test.web.MyInteceptor" />
    </mvc:interceptors>

    拦截器,拦截所有url。

    心血来潮,稍微研究了一下DispatcherServlet。所知有限,这里就不介绍更多。

    以下为所有参考的文章:

    第三章 DispatcherServlet详解 ——跟开涛学SpringMVC
    http://jinnianshilongnian.iteye.com/blog/1602617

    web.xml配置文件中的servlet和servlet-mapping
    http://blog.csdn.net/u013815649/article/details/50435819

    使用@Controller注解为什么要配置<mvc:annotation-driven />
    http://blog.csdn.net/jbgtwang/article/details/7359592

    spring mvc拦截器和<mvc:annotation-driven />的详解
    http://www.cnblogs.com/yangzhilong/p/3725849.html

    Spring组件扫描<context:component-scan/>使用详解
    http://www.cnblogs.com/maybo/p/5189516.html

    <context:component-scan>使用说明
    http://blog.csdn.net/chunqiuwei/article/details/16115135

    Spring MVC 教程,快速入门,深入分析(推荐阅读)

    http://elf8848.iteye.com/blog/875830

  • 相关阅读:
    Best Time to Buy and Sell Stock(动态规划)
    word break
    Container With Most Water
    Partition List(链表的插入和删除操作,找前驱节点)
    取样问题(编程珠玑)
    统计指标
    脚本化加载文件与转储
    azkaban调度
    hive自定义UDTF函数叉分函数
    hive数据仓库建设
  • 原文地址:https://www.cnblogs.com/zj0208/p/6233558.html
Copyright © 2011-2022 走看看