Spring MVC : 概念模型 HandlerAdapter

HandlerAdapter作为SpringMVC框架的SPI,允许参数化核心MVC工作流,为每种handler类型提供适配器,使DispatcherServlet可无限扩展。它支持任意类型的handler对象,包括其他框架的handler及通过注解集成的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HandlerAdapterSpring MVC框架的SPI(服务提供者接口),用来参数化核心MVC工作流。

对于处理一个请求的每种handler类型,都要有一个对应的HandlerAdapter实现(一个HandlerAdapter可以支持多种handler)。该接口的一个目的是让前置DispatcherServlet可以被无限扩展。因为DispatcherServlethandler的访问可以通过该接口而无需关注每种handler类型的代码实现细节。

一个handler可以是任意类型的Object。这么设计的目的是其他框架的handler可以不用改造就可以集成到Spring MVC框架中来,或者是某个handler对象即使不使用任何Java接口只要通过注解也能被集成进来。

HandlerAdapter接口不是给应用开发人员用的。Spring MVC框架自己提供了一些实现,另外该接口可以用于开发有特定流程要求的handler

HandlerAdapter 接口其实只定义了三个方法:

  • boolean supports(Object handler) – 是否支持指定的handler

典型实现举例 : return (handler instanceof MyHandler)

  • ModelAndView handle(HttpServletRequest, HttpServletResponse, Object handler) – 调用handler处理请求

    • 返回一个ModelAndView对象,带有view名称和所需的模型数据
    • 如果handler处理过程中请求被处理完成,返回结果会是null
    • 错误的话会抛出异常
  • long getLastModified(HttpServletRequest request, Object handler)

    • HttpServlet的方法getLastModified语义相同
    • 返回资源的lastModified信息,handler根据自己的逻辑定义决定返回什么样的值
    • 如果handler不支持该语义,可直接返回-1

源代码

package org.springframework.web.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.lang.Nullable;

/**
 * MVC framework SPI, allowing parameterization of the core MVC workflow.
 *
 * Interface that must be implemented for each handler type to handle a request.
 * This interface is used to allow the DispatcherServlet to be indefinitely
 * extensible. The DispatcherServlet accesses all installed handlers through
 * this interface, meaning that it does not contain code specific to any handler type.
 *
 * 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-driven handler objects that
 * do not obey any specific Java interface.
 *
 * This interface is not intended for application developers. It is available
 * to handlers who want to develop their own web workflow.
 *
 * Note: HandlerAdapter implementors may implement the
 * org.springframework.core.Ordered interface to be able to specify a sorting
 * order (and thus a priority) for getting applied by the DispatcherServlet.
 * Non-Ordered instances get treated as lowest priority.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @see org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
 * @see org.springframework.web.servlet.handler.SimpleServletHandlerAdapter
 */
public interface HandlerAdapter {

	/**
	 * Given a handler instance, return whether or not this HandlerAdapter
	 * can support it. Typical HandlerAdapters will base the decision on the handler
	 * type. HandlerAdapters will usually only support one handler type each.
	 * A typical implementation:
	 * 
	 * return (handler instanceof MyHandler);
	 * 
	 * @param handler handler object to check
	 * @return whether or not this object can use the given handler
	 */
	boolean supports(Object handler);

	/**
	 * Use the given handler to handle this request.
	 * The workflow that is required may vary widely.
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @param handler handler to use. This object must have previously been passed
	 * to the supports method of this interface, which must have
	 * returned true.
	 * @throws Exception in case of errors
	 * @return a ModelAndView object with the name of the view and the required
	 * model data, or null if the request has been handled directly
	 */
	@Nullable
	ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) 
		throws Exception;

	/**
	 * Same contract as for HttpServlet's getLastModified method.
	 * Can simply return -1 if there's no support in the handler class.
	 * @param request current HTTP request
	 * @param handler handler to use
	 * @return the lastModified value for the given handler
	 * @see javax.servlet.http.HttpServlet#getLastModified
	 * @see org.springframework.web.servlet.mvc.LastModified#getLastModified
	 */
	long getLastModified(HttpServletRequest request, Object handler);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值