spring-webmvc-DispatcherServlet
Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning.
Spring MVC 是基于Servlet API 的原生Web framework。之前对这个有误解,以为Spring MVC是另起炉灶的、新的web framework ,随着对Spring MVC的
了解才认识到这一点。
Spring MVC, as many other web frameworks, is designed around the front controller pattern where a central Servlet
, the DispatcherServlet
, provides a shared algorithm for request processing, while actual work is performed by configurable delegate components. This model is flexible and supports diverse workflows.
The DispatcherServlet
, as any Servlet
, needs to be declared and mapped according to the Servlet specification by using Java configuration or in web.xml
. In turn, the DispatcherServlet
uses Spring configuration to discover the delegate components it needs for request mapping, view resolution, exception handling, and more.
DispatcherServlet 使用Java configuration 或者
web.xml配置。
The following example of the Java configuration registers and initializes the DispatcherServlet
, which is auto-detected by the Servlet container (see Servlet Config):
使用Java configuration
时,实现WebApplicationInitializer 接口,不需要额外的配置,会自动被 Servlet 容器 (比如Tomcat) 发现。
public class MyWebApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletCxt) { // Load Spring web application configuration AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext(); ac.register(AppConfig.class); ac.refresh(); // Create and register the DispatcherServlet DispatcherServlet servlet = new DispatcherServlet(ac); ServletRegistration.Dynamic registration = servletCxt.addServlet("app", servlet); registration.setLoadOnStartup(1); registration.addMapping("/app/*"); } }
In addition to using the ServletContext API directly, you can also extend AbstractAnnotationConfigDispatcherServletInitializer
and override specific methods (see the example under Context Hierarchy).
<web-app> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/app-context.xml</param-value> </context-param> <servlet> <servlet-name>app</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>app</servlet-name> <url-pattern>/app/*</url-pattern> </servlet-mapping> </web-app>
Spring Boot follows a different initialization sequence. Rather than hooking into the lifecycle of the Servlet container, Spring Boot uses Spring configuration to bootstrap itself and the embedded Servlet container. Filter
and Servlet
declarations are detected in Spring configuration and registered with the Servlet container. For more details, see the Spring Boot documentation.
Context Hierarchy
DispatcherServlet
expects a WebApplicationContext
(an extension of a plain ApplicationContext
) for its own configuration. WebApplicationContext
has a link to the ServletContext
and the Servlet
with which it is associated. It is also bound to the ServletContext
such that applications can use static methods on RequestContextUtils
to look up the WebApplicationContext
if they need access to it.
(WebApplicationContext
和 ServletContext
"绑定",可以通过 RequestContextUtils 获得
WebApplicationContext。
For many applications, having a single WebApplicationContext
is simple and suffices. It is also possible to have a context hierarchy where one root WebApplicationContext
is shared across multiple DispatcherServlet
(or other Servlet
) instances, each with its own child WebApplicationContext
configuration. See Additional Capabilities of the ApplicationContext
for more on the context hierarchy feature.
The root WebApplicationContext
typically contains infrastructure beans, such as data repositories and business services that need to be shared across multiple Servlet
instances. Those beans are effectively inherited and can be overridden (that is, re-declared) in the Servlet-specific child WebApplicationContext
, which typically contains beans local to the given Servlet
. The following image shows this relationship:
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { RootConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { App1Config.class }; } @Override protected String[] getServletMappings() { return new String[] { "/app1/*" }; } }
1.1.2. Special Bean Types
The DispatcherServlet
delegates to special beans to process requests and render the appropriate responses. By “special beans” we mean Spring-managed Object
instances that implement framework contracts. Those usually come with built-in contracts, but you can customize their properties and extend or replace them.
spring-webmvc-DispatcherServlet的更多相关文章
- Tutorial: Build a Spring WebMVC App with Primefaces
Tutorial: Build a Spring WebMVC App with Primefaces by Team Stormpath | September 7, 2016 | Java Pri ...
- servlet和Spring的DispatcherServlet详解
Servlet是什么 1. Servlet是服务器端运行的一个程序,是一个被编译好的Java类.它不是框架等. 2. Web容器的启动,需要依赖Servlet.当web服务器开始执行时,servlet ...
- spring mvc DispatcherServlet详解之前传---FrameworkServlet
做项目时碰到Controller不能使用aop进行拦截,从网上搜索得知:使用spring mvc 启动了两个context:applicationContext 和WebapplicationCont ...
- (转载)spring mvc DispatcherServlet详解之一---处理请求深入解析
要深入理解spring mvc的工作流程,就需要先了解spring mvc的架构: 从上图可以看到 前端控制器DispatcherServlet在其中起着主导作用,理解了DispatcherServl ...
- 【转载】Spring中DispatcherServlet与ContextLoaderListener的区别
昨天在写springmvc的时候,在web.xml中配置了DispatcherServlet,如下: <servlet> <servlet-name>DispatcherSer ...
- spring mvc DispatcherServlet详解之三---request通过ModelAndView中获取View实例的过程
整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet第二步:通过request从Controller获取ModelAndView.现在来讲解第三步:reques ...
- spring mvc DispatcherServlet详解之拾忆工具类utils
DispatcherServlet的静态初始化 /** * Name of the class path resource (relative to the DispatcherServlet cla ...
- spring mvc DispatcherServlet详解之二---request通过Controller获取ModelAndView过程
整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet通过request获取控制器Controller的过程,现在来讲解DispatcherServletDisp ...
- spring mvc DispatcherServlet详解之一--request通过HandlerMaping获取控制器Controller过程
整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的第一步:获取控制器. HandlerMapping HandlerMappi ...
- spring mvc DispatcherServlet详解之四---视图渲染过程
整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的最后一步:视图渲染.视图渲染的过程是在获取到ModelAndView后的过程 ...
随机推荐
- IntelliJ IDEA下如何设置JSP模板
今天在学习Spring MVC知识时,发现自己所用的IntelliJ IDEA中自动生成的JSP文件不支持EL表达式的使用,所以就想导入新的JSP模板,方便以后使用.根据旧模板的提示,如下图 找到Se ...
- iOS视频边下载边播放
随着视频行业的发展,很多用户对于观看体验也有了更高的要求,以前的习惯是下载好了在观看,而现在是希望1分钟都不要等,ZUI好一边看着一边下载,等把这个视频看完也下载完了,也就是我们常说的“视频边下载边播 ...
- 学习笔记—MySQL基础
数据库的介绍 mysql数据库介绍 开放源码的轻量级关系型数据库管理系统,体积小.速度快.操作便捷. 数据库的启动和连接 mysql数据库启动 在终端输入以下命令,启动mysql服务器 service ...
- 【设计模式】桥接模式 Bridge Pattern
开篇还是引用吕振宇老师的那篇经典的文章<设计模式随笔-蜡笔与毛笔的故事>.这个真是太经典了,没有比这个例子能更好的阐明桥接模式了,这里我就直接盗来用了. 现在市面上卖的蜡笔很多,各种型号, ...
- fullpage 全屏插件
fullpage 全屏插件 全屏滚动效果,原生js也很好实现,主要是用 mousewheel 鼠标滚轮滚动事件, 来判断上滚动还是下滚动,之后设置每次滚动的高度为屏幕的高度即可.但是,虽然效果简单, ...
- android开发中调用python代码(带参数)
android开发主要用到的是java代码,但是当开发涉及到一些算法时,往往用python可以提高软件的运行速度,也更加便捷,这里分享自己项目调用python代码的方式,主要有以下几个步骤(个人方法, ...
- java:数据结构(二)栈的应用(括号匹配)
一.什么是括号匹配: 括号匹配就是利用计算机辨别表达式里面的括号是否书写成功 例如: {()((a)) }这就是一个正确 (()() 这就是一个错误的 二.括号匹配的算法: 众所周知,括号分为花括 ...
- win10的hyper-v共享文件夹
win10这个奇葩,共享文件夹变得非常困难. 其他不说,关键点在于获取虚拟机的 ip,我的虚拟机系统是winxp,虚拟机本身可以上网.拿到ip,用这个ip还是无法访问\\ip 的共享文件夹,因为win ...
- BASE64编码的图片在网页中的显示问题的解决
BASE64位转码有两种: 一种是图片转为Base64编码,这种编码是直接可以在页面通过<img src='base64编码'/>的方式显示 Base64 在CSS中的使用 .demoIm ...
- 深入浅出KNN算法(一) KNN算法原理
一.KNN算法概述 KNN可以说是最简单的分类算法之一,同时,它也是最常用的分类算法之一,注意KNN算法是有监督学习中的分类算法,它看起来和另一个机器学习算法Kmeans有点像(Kmeans是无监督学 ...