java Spring MVC 中的 Controller 和 Handler 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30165625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:35:32  来源:igfitidea点击:

what's difference between Controller and Handler in Spring MVC?

javaspringspring-mvc

提问by user983447

The documentation of Spring MVC sometimes says about "handlers" or "request handlers". For instance, http://docs.spring.io/autorepo/docs/spring/4.0.4.RELEASE/javadoc-api/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.htmlsays:

Spring MVC 的文档有时会提到“处理程序”或“请求处理程序”。例如,http: //docs.spring.io/autorepo/docs/spring/4.0.4.RELEASE/javadoc-api/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.html说:

Implementation of the HandlerMapping interface to map from URLs to request handler beans

HandlerMapping 接口的实现以从 URL 映射到请求处理程序 bean

And sometimes it says about controllers. For instance, there is an interface called org.springframework.web.servlet.mvc.Controller ( http://docs.spring.io/spring-framework/docs/2.5.x/api/org/springframework/web/servlet/mvc/Controller.html).

有时它会提到控制器。例如,有一个名为 org.springframework.web.servlet.mvc.Controller ( http://docs.spring.io/spring-framework/docs/2.5.x/api/org/springframework/web/servlet/ mvc/Controller.html)。

My question is: are Controllers and Handlers the same?

我的问题是:控制器和处理程序相同吗?

回答by MChaker

Generally speaking, a Controller is Handler, but a Handler doesn't have to be a Controller.

一般来说,Controller就是Handler,但Handler不一定是Controller。

For example, HttpRequestHandler, WebRequestHandler, MessageHandlerare all handlers which can work with the DispatcherServlet. ( (@)Controller is a handler for executing a web request and returning a view.)

例如HttpRequestHandlerWebRequestHandlerMessageHandler是可以通过工作的所有处理程序DispatcherServlet。( ( @)Controller 是用于执行 Web 请求并返回视图的处理程序。)

Shortly, Handler is just a term, it is neither a class nor interface. And it is responsible for executing the Mapping.

简而言之,Handler 只是一个术语,它既不是类也不是接口。它负责执行映射。

回答by M. Deinum

A Controlleris a specific type of Handlerbut not all Handlers are Controllers.

AController是s的特定类型,Handler但并非所有Handlers 都是Controllers。

To execute a type of Handlerthere is a HandlerAdapterand for each type of Handlerthere is a different HandlerAdapter. You have Controllerand @Controller, HttpRequestHandlerand also a plain Servletcan be a Handler. Or if you have some custom things you can even implement your own.

执行一种类型Handler有一个,HandlerAdapter并且对于每种类型Handler都有一个不同的HandlerAdapter. 你有Controller@ControllerHttpRequestHandlerServlet可以是一个普通的Handler。或者,如果您有一些自定义的东西,您甚至可以实现自己的东西。

回答by Premraj

Handleris a inclusive i.e. covering all the services details.
Controlleris an an exclusive implementation.

Handler是一个包容性的 ie,涵盖了所有的服务细节。
控制器是一个独占的实现。

In Spring we have the following different types of handlers:

在 Spring 中,我们有以下不同类型的处理程序:

  • HandlerMapping: The HandlerMappingstrategy is used to mapthe HTTP client request to some handler controller(or controllers) and/or method. This is done based on the request URL and the HTTP method, but may also include the request parameters, request headers, or other custom factors.
    For example:DefaultAnnotationHandlerMapping, SimpleUrlHandlerMapping, BeanNameUrlHandlerMapping.
  • HandlerAdapter: The DispatcherServletuses a HandlerAdapter to invokea method. Which is decouples the DispatcherServletfrom controller implementations classes.
    For example:AnnotationMethodHandlerAdapter, HttpRequestHandlerAdapter, RequestMappingHandlerAdapter, SimpleControllerHandlerAdapter, SimpleServletHandlerAdapter
  • HandlerMapping:该HandlerMapping策略用于将 HTTP 客户端请求映射到某个处理程序控制器(或控制器)和/或方法。这是基于请求 URL 和 HTTP 方法完成的,但也可能包括请求参数、请求标头或其他自定义因素。
    例如:DefaultAnnotationHandlerMapping, SimpleUrlHandlerMapping, BeanNameUrlHandlerMapping
  • HandlerAdapter:DispatcherServlet使用 HandlerAdapter 来调用一个方法。这是DispatcherServlet从控制器实现类中解耦的。
    例如:AnnotationMethodHandlerAdapter, HttpRequestHandlerAdapter, RequestMappingHandlerAdapter, SimpleControllerHandlerAdapter,SimpleServletHandlerAdapter