在上一篇我们介绍了Servlet,这一篇主要来看一下MVC中用到的DispatcherServlet(继承自HttpServlet)。

1. DispatcherServlet在web.xml中被声明。

<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/example/*</url-pattern>
</servlet-mapping> </web-app>

注:我们也可以在代码中去进行设置,具体参考http://docs.spring.io/spring/docs/4.0.6.RELEASE/spring-framework-reference/htmlsingle/#mvc-container-config

2. DispatcherServlet包含的一些beans

3. 除了步骤2种默认提供的bean以外,Spring MVC还会在WEB-INF文件夹下寻找一个 [servlet-name]-servlet.xml 文件(步骤1中,会寻找example-servlet.xml这个文件), 创建这个文件中定义的beans。

如果需要修改默认的bean,可以在这个里面进行定义,加载时会覆盖默认的bean。

4. 我们可以通过DispatcherServlet中的初始化方法,配置步骤3中的xml的路径。

或者,如果你想用一个公用的xml,可以配置context-param并且不设置contextConfigLocation的值就可以了。

<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

5. 如果要让mvc下的annotation起作用,需要在 [servlet-name]-servlet.xml 中增加一行。

<?xml version=1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven /> </beans>

最新文章

  1. Web 开发人员系统重装备忘录
  2. git删除分支|查看分支动态
  3. WebDriver兼容SeleniumRC(基于C#)
  4. linux 挂载(转载)
  5. ubuntu 上使用apt-get安装oracle-jdk
  6. mysql引擎互转问题
  7. 使用2to3将代码移植到Python 3-转
  8. DirectX 11游戏编程学习笔记之8: 第6章Drawing in Direct3D(在Direct3D中绘制)(习题解答)
  9. hdr(host), hdr_beg(host) , path_beg
  10. CSS属性值定义语法中的符号说名
  11. python基本数据类型——dict
  12. hibernate 根据数据库表反生成javaBean
  13. maven项目部署对Oracle jar包的处理
  14. 【重点--web前端面试题总结】
  15. the shortest path algorithm
  16. 图解Golang的GC算法
  17. Hibernate中的Entity类之间的继承关系之一MappedSuperclass
  18. 异步陷阱之IO篇
  19. phpStudy集成环境下 安装composer
  20. 双数组Trie的一种实现

热门文章

  1. 用unity3d切割图片
  2. 【Java】Java_14 循环结构
  3. Java 遍历一个对象的属性 将非空属性赋值给另一个对象
  4. MATLAB 的通用命令
  5. hiredis学习整理
  6. rtp协议详解/rtcp协议详解
  7. 固态继电器SSR
  8. Redis之ziplist数据结构
  9. 21. Merge Two Sorted Lists【easy】
  10. 内核并发管理---spin lock