spring mvc中的Handler适配器是什么?
我是 Spring MVC 的初学者。我不太了解处理程序适配器。什么是处理程序适配器以及何时使用适配器?
I am beginner in Spring MVC. I didn't understand handler adapters clearly. What is a handler adapter and when do I use adapters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
HandlerMapping
将方法映射到 URL,因此DispatcherServlet
知道特定请求应调用哪个方法。然后,DispatcherServlet 使用 HandlerAdapter 来调用该方法。因为调用方法的方式有很多种,比如注释、xml 等。
HandlerAdapter
解耦了DispatcherServlet
和调用的操作。A
HandlerMapping
maps a method to a URL, so theDispatcherServlet
knows which method should be invoked by a specific request. Then theDispatcherServlet
use aHandlerAdapter
to invoke the method.Because there are many ways to invoke a method, like annotation, xml etc.
HandlerAdapter
de-couples theDispatcherServlet
and the invoked actions.本节 Spring 文档的 部分讨论了默认适配器以及它们与注释配置的关系。
简而言之,处理程序适配器决定为请求调用哪个控制器(和方法)。
This section of the Spring docs discusses the default adapters and how they relate to annotation configuration.
Briefly, handler adapters decide which controller (and method) to call for a request.
您可以在 HandlerAdapter 中找到 Adapter,这部分名称来自 Adapter 模式。 Adapter 就像两个对象之间的桥梁,HandlerAdapter 是处理程序对象和调度程序 servlet 之间的桥梁。
从下面取自 Spring 文档的 HandlerAdapter 源代码中可以看到,有一个方法,即带有 ModelAndView 返回类型的 handle 方法。每个 HandlerAdapter 将实现此方法以将 HttpServletRequest 和 HttpServletResponse 委托给处理程序对象,以便处理程序对象将使用这些 HttpServletRequest/Response 执行应用程序逻辑。
该应用程序逻辑执行产生模型和视图。视图可以是视图名称字符串或视图对象的形式。该模型保存将用于渲染视图的数据。 HandlerAdapter 将模型和视图包装在 ModelAndView 对象中。处理 ModelAndView 对象是调度程序 servlet 的工作。
调度程序 Servlet 不了解处理程序对象,并且无需直接处理应用程序逻辑。 Handler 对象也无需将模型和视图转换为 ModelAndView 对象,因为 HandlerAdapter 将完成该转换工作。
You can find Adapter in HandlerAdapter and that part of name comes from Adapter pattern. Adapter is like a bridge between two objects and HandlerAdapter is a bridge between handler object and dispatcher servlet.
As you can see from the HandlerAdapter source code below taken from Spring documentation, there is one method, handle method with ModelAndView return type. Every HandlerAdapter will implement this method to delegate the HttpServletRequest and HttpServletResponse to handler object so then handler object will execute application logic using these HttpServletRequest/Response.
This application logic execution produces model and view. The view can be in form of view name String or View object. The model holds data that will be used to render the view. HandlerAdapter will wrap the model and view in ModelAndView object. It is dispatcher servlet job to process ModelAndView object.
Dispatcher servlet does not know about handler object and relieved from directly handle application logic. Handler object also relieved from converting model and view into ModelAndView object because HandlerAdapter will do that converting job.
HandlerMapping 只是将方法映射到 URL。大多数初学者不会直接使用这个对象,而是使用 RequestMapping 的。映射方法的返回类型(通常)决定了 SpringMVC 将使用哪个视图来呈现响应。
例如,以下 RequestMapping 将为“/”或“/home”的 GET 请求生成一个 HandlerMapping 来调用此方法:
该方法返回视图的字符串名称,通常会解析为“/WEB-INF/views/homepage.jsp”(但这取决于您的 ViewResolver 当然)
仅供参考:您可以添加可能需要的不同对象作为方法的参数(例如 Locale、 HttpServletRequest 等)。有关详细信息,请参阅 RequestMapping javadoc。
A HandlerMapping simply maps a method to a URL. Most beginners don't use this object directly, but rather use RequestMapping's instead. The return type of the mapped method (generally) determines what view SpringMVC will use to render a response.
For example, the following RequestMapping will generate a HandlerMapping for GET requests to "/" or "/home" to invoke this method:
The method returns a string name of a view, which would typically be resolved to "/WEB-INF/views/homepage.jsp" (but that depends on your ViewResolver of course)
Just an fyi for starting out: you can add different objects that you might need as parameters to your method (like Locale, HttpServletRequest, etc). See the RequestMapping javadoc for more info.
从 Spring docs
它遵循 SPI 设计模式
它有助于与其他框架集成(因为我们不直接使用处理程序对象并使用适配器来调用。这意味着适配器负责使用处理程序的其他管道逻辑)
重要的是我们应该注意它不适合应用程序开发人员:
From the Spring docs
It follow SPI design pattern
It helps with integrating with other frameworks (because since we are not directly using handler object and using the adapter to invoke. This means the adapter takes care of other plumbing logic for using handler)
Importantly we should note that it is not for application developers: