简体   繁体   中英

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

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.html says:

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

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 ).

My question is: are Controllers and Handlers the same?

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

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

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

A Controller is a specific type of Handler but not all Handler s are Controller s.

To execute a type of Handler there is a HandlerAdapter and for each type of Handler there is a different HandlerAdapter . You have Controller and @Controller , HttpRequestHandler and also a plain Servlet can be a Handler . Or if you have some custom things you can even implement your own.

Handler is a inclusive ie covering all the services details.
Controller is an an exclusive implementation.

In Spring we have the following different types of handlers:

  • HandlerMapping : The HandlerMapping strategy is used to map the 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 DispatcherServlet uses a HandlerAdapter to invoke a method. Which is decouples the DispatcherServlet from controller implementations classes.
    For example: AnnotationMethodHandlerAdapter , HttpRequestHandlerAdapter , RequestMappingHandlerAdapter , SimpleControllerHandlerAdapter , SimpleServletHandlerAdapter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM