美文网首页程序员
Spring MVC 系列之DispatcherServlet

Spring MVC 系列之DispatcherServlet

作者: Jacky_2c9f | 来源:发表于2020-05-23 18:30 被阅读0次

    今天我们来翻译一下大名鼎鼎的 DispatcherServlet 的官方文档。目前最新版的包为 spring-webmvc-5.2.6.RELEASE.jar, 类路径为 org.springframework.web.servlet.DispatcherServlet。

    正文开始预告

    ————————————————————————————————————————

    Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers or HTTP-based remote service exporters. Dispatches to registered handlers for processing a web request, providing convenient mapping and exception handling facilities.

    核心转发器,用于HTTP请求的处理器/控制器。比如,用于Web UI 的控制器或者基于HTTP的远程服务输出器。转发到已注册的处理器去处理一个Web 请求,提供方便快捷的映射和异常处理机制。

    This servlet is very flexible: It can be used with just about any workflow, with the installation of the appropriate adapter classes. It offers the following functionality that distinguishes it from other request-driven web MVC frameworks:

    DispatcherServlet 非常方便。它可用于任何工作流,只要安装适当的适配器类即可。他同时提供了以下功能,这很明显将它与其他请求驱动的Web MVC框架区分开。

    It is based around a JavaBeans configuration mechanism.

    它是基于一种JavaBeans 配置的机制。

    It can use any HandlerMapping implementation - pre-built or provided as part of an application - to control the routing of requests to handler objects. Default is BeanNameUrlHandlerMapping and RequestMappingHandlerMapping. HandlerMapping objects can be defined as beans in the servlet's application context, implementing the HandlerMapping interface, overriding the default HandlerMapping if present. HandlerMappings can be given any bean name (they are tested by type).

    (注意这里跟以前的版本不一样,在5之前的版本用的是org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping.)

    它可以使用任何HandlerMapping实现(预先构建或作为应用程序的一部分提供)来控制将请求路由到处理程序对象。 默认是org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping 和 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping。可以在servlet 的应用程序上下文里面将 HandlerMapping 对象定义为beans, 实现HandlerMapping 接口,覆盖默认的HandlerMapping(假如存在的话)。可以给HandlerMappings 任意一个bean的名字(它们是通过类型测试的)。

    It can use any HandlerAdapter; this allows for using any handler interface. Default adapters are HttpRequestHandlerAdapterSimpleControllerHandlerAdapter, for Spring's HttpRequestHandler and Controller interfaces, respectively. A default RequestMappingHandlerAdapter will be registered as well. HandlerAdapter objects can be added as beans in the application context, overriding the default HandlerAdapters. Like HandlerMappings, HandlerAdapters can be given any bean name (they are tested by type).

    它可以使用任何 HandlerAdapter;这允许使用任何处理器接口。默认适配器是org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,

    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter, 分别用于Spring的org.springframework.web.HttpRequestHandler and org.springframework.web.servlet.mvc.Controller 这两个接口。默认的RequestMappingHandlerAdapter 也会被注册。在应用程序上下文中可以将 HandlerAdapter 对象加到 beans 里面,以覆盖默认的 HandlerAdapters。像HandlerMappings 一样, HandlerAdapters bean 也可以取任意一个名字(它们是通过类型测试的)。

    The dispatcher's exception resolution strategy can be specified via a HandlerExceptionResolver, for example mapping certain exceptions to error pages. Default are ExceptionHandlerExceptionResolverResponseStatusExceptionResolver, and DefaultHandlerExceptionResolver. These HandlerExceptionResolvers can be overridden through the application context. HandlerExceptionResolver can be given any bean name (they are tested by type).

    可以通过 HandlerExceptionResolver 这个接口来指定调度程序的异常解决策略, 比如将某些异常映射到错误页面. 默认有以下几个实现类 ExceptionHandlerExceptionResolver,ResponseStatusExceptionResolver, 和DefaultHandlerExceptionResolver. 这些控制异常处理 bean 同样也可以取任意名字(它们是通过类型测试的)。

    Its view resolution strategy can be specified via a ViewResolver implementation, resolving symbolic view names into View objects. Default is InternalResourceViewResolver. ViewResolver objects can be added as beans in the application context, overriding the default ViewResolver. ViewResolvers can be given any bean name (they are tested by type).

    可以通过 ViewResolver 的实现类来指定其视图解析策略,将符号视图名字解析为 View 对象。默认的实现类是 InternalResourceViewResolver 。同样可以将 ViewResolver 对象 作为 bean 添加到 application context 中,覆盖默认的 ViewResolver. 这些 ViewResolvers bean 同样也可以取任意名字(它们是通过类型测试的)。

    If a View or view name is not supplied by the user, then the configured RequestToViewNameTranslator will translate the current request into a view name. The corresponding bean name is "viewNameTranslator"; the default is DefaultRequestToViewNameTranslator.

    假如用户没有提供一个 View 对象 或 视图名称, 那么已配置的 RequestToViewNameTranslator 接口(实现类)会将当前的请求转换为视图名称。其对应的 bean 名称为 "viewNameTranslator", 默认的实现类为 DefaultRequestToViewNameTranslator.

    The dispatcher's strategy for resolving multipart requests is determined by a MultipartResolver implementation. Implementations for Apache Commons FileUpload and Servlet 3 are included; the typical choice is CommonsMultipartResolver. The MultipartResolver bean name is "multipartResolver"; default is none.

    dispatcherServlet 的解析多部分请求(比如请求中同时包含JSON和文件)策略是由 MultipartResolver 的实现类决定的。其中包括了对 Apache Commons FileUpload 和 Servlet 3.0 的实现,比较典型的选择是使用 CommonsMultipartResolver 。MultipartResolver bean 的名称为 "multipartResolver", 默认不使用。

    Its locale resolution strategy is determined by a LocaleResolver. Out-of-the-box implementations work via HTTP accept header, cookie, or session. The LocaleResolver bean name is "localeResolver"; default is AcceptHeaderLocaleResolver.

    其语言环境解析策略是由 LocaleResolver 确定。现成的实现类是通过 HTTP accept 头部, cookie 或者 session 来工作的。该 LocaleResolver  的 bean 名称为 "localeResolver", 默认的实现类为 AcceptHeaderLocaleResolver.

    Its theme resolution strategy is determined by a ThemeResolver. Implementations for a fixed theme and for cookie and session storage are included. The ThemeResolver bean name is "themeResolver"; default is FixedThemeResolver.

    其主题解析策略由 ThemeResolver 确定。包括了对一个固定主题,cookie 以及 session storage 的实现。ThemeResolver Bean名称为“ themeResolver”;默认值为FixedThemeResolver.

    NOTE: The @RequestMapping annotation will only be processed if a corresponding HandlerMapping (for type-level annotations) and/or HandlerAdapter (for method-level annotations) is present in the dispatcher. This is the case by default. However, if you are defining custom HandlerMappings or HandlerAdapters, then you need to make sure that a corresponding custom RequestMappingHandlerMapping and/or RequestMappingHandlerAdapter is defined as well - provided that you intend to use @RequestMapping.

    注意:@RequestMapping 注解只有在对应的HandlerMapping (类型级别的声明) 和/或 HandlerAdapter(方法级别的声明) 存在于该dispatcher中的时候才会被处理。默认就是这种情况。然而,假如你定义了自己的 HandlerMappings 或者是 HandlerAdapters,那么你需要确保你也同时定义了相对应的 RequestMappingHandlerMapping 和/或 RequestMappingHandlerAdapter, 前提是你打算使用 @RequestMapping 注解。

    A web application can define any number of DispatcherServlets. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by org.springframework.web.context.ContextLoaderListener, if any, will be shared.

    Web应用程序可以定义任意数量的DispatcherServlet。 每个servlet将在其自己的命名空间中运行,使用映射,处理器等加载自己的应用程序上下文。只有由org.springframework.web.context.ContextLoaderListener加载的根应用程序上下文(如果有的话)将被共享。

    As of Spring 3.1, DispatcherServlet may now be injected with a web application context, rather than creating its own internally. This is useful in Servlet 3.0+ environments, which support programmatic registration of servlet instances. See DispatcherServlet(WebApplicationContext) Javadoc for details.

    从Spring 3.1开始,DispatcherServlet可以被注入Web应用程序上下文,而不是在内部创建它。这在Servlet 3.0+环境中很有用,它支持servlet实例的编程注册,详情可参考DispatcherServlet(WebApplicationContext)。

    以上资料均翻译自官方文档,版本为5.2.6.RELEASE。

    https://docs.spring.io/spring/docs/5.2.6.RELEASE/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html

    https://docs.spring.io/spring/docs/4.3.9.RELEASE/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html

    相关文章

      网友评论

        本文标题:Spring MVC 系列之DispatcherServlet

        本文链接:https://www.haomeiwen.com/subject/ivtlahtx.html