spring mvc中的Handler适配器是什么?

发布于 2024-12-11 07:00:35 字数 59 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月静好 2024-12-18 07:00:35

HandlerMapping 将方法映射到 URL,因此 DispatcherServlet 知道特定请求应调用哪个方法。然后,DispatcherServlet 使用 HandlerAdapter 来调用该方法。

为什么DispatcherServlet不直接调用方法?

因为调用方法的方式有很多种,比如注释、xml 等。HandlerAdapter 解耦了 DispatcherServlet 和调用的操作。

A HandlerMapping maps a method to a URL, so the DispatcherServlet knows which method should be invoked by a specific request. Then the DispatcherServlet use a HandlerAdapter to invoke the method.

Why DispatcherServlet does not invoke a method directly?

Because there are many ways to invoke a method, like annotation, xml etc. HandlerAdapter de-couples the DispatcherServlet and the invoked actions.

暮光沉寂 2024-12-18 07:00:35

本节 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.

吲‖鸣 2024-12-18 07:00:35

您可以在 HandlerAdapter 中找到 Adapter,这部分名称来自 Adapter 模式。 Adapter 就像两个对象之间的桥梁,HandlerAdapter 是处理程序对象和调度程序 servlet 之间的桥梁。

从下面取自 Spring 文档的 HandlerAdapter 源代码中可以看到,有一个方法,即带有 ModelAndView 返回类型的 handle 方法。每个 HandlerAdapter 将实现此方法以将 HttpServletRequest 和 HttpServletResponse 委托给处理程序对象,以便处理程序对象将使用这些 HttpServletRequest/Response 执行应用程序逻辑。

public interface HandlerAdapter {

  //Check if controller is supported
  boolean supports(Object handler);   

  //handle request
  ModelAndView handle(HttpServletRequest rqst, 
                      HttpServletResponse rsp,
                      Object handler) throws Exception; 

该应用程序逻辑执行产生模型和视图。视图可以是视图名称字符串或视图对象的形式。该模型保存将用于渲染视图的数据。 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.

public interface HandlerAdapter {

  //Check if controller is supported
  boolean supports(Object handler);   

  //handle request
  ModelAndView handle(HttpServletRequest rqst, 
                      HttpServletResponse rsp,
                      Object handler) throws Exception; 

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.

顾铮苏瑾 2024-12-18 07:00:35

HandlerMapping 只是将方法映射到 URL。大多数初学者不会直接使用这个对象,而是使用 RequestMapping 的。映射方法的返回类型(通常)决定了 SpringMVC 将使用哪个视图来呈现响应。

例如,以下 RequestMapping 将为“/”或“/home”的 GET 请求生成一个 HandlerMapping 来调用此方法:

@RequestMapping(value={"/", "/home"}, method=RequestMethod.GET)
public String getHome() {
    return "homepage";
}

该方法返回视图的字符串名称,通常会解析为“/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:

@RequestMapping(value={"/", "/home"}, method=RequestMethod.GET)
public String getHome() {
    return "homepage";
}

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.

银河中√捞星星 2024-12-18 07:00:35

从 Spring docs

它遵循 SPI 设计模式

MVC框架SPI接口,允许核心MVC参数化
工作流程。

它有助于与其他框架集成(因为我们不直接使用处理程序对象并使用适配器来调用。这意味着适配器负责使用处理程序的其他管道逻辑)

请注意,处理程序可以是对象类型。这是为了启用处理程序
来自其他框架的内容无需与此框架集成
自定义编码,以及允许注释处理程序对象
不遵守任何特定的 Java 接口。

重要的是我们应该注意它不适合应用程序开发人员:

此界面不适用于应用程序开发人员。它可供想要开发自己的 Web 工作流程的处理程序使用。

From the Spring docs

It follow SPI design pattern

MVC framework SPI interface, allowing parameterization of core MVC
workflow.

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)

Note that a handler can be of type Object. This is to enable handlers
from other frameworks to be integrated with this framework without
custom coding, as well as to allow for annotation handler objects that
do not obey any specific Java interface.

Importantly we should note that it is not for application developers:

This interface is not intended for application developers. It is available to handlers who want to develop their own web workflow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文