当 Spring MVC 收到客户端的 HTTP 请求后,会按照以下步骤处理请求:
前端控制器 DispatcherServlet 接收请求:
客户端发送的 HTTP 请求首先被前端控制器 DispatcherServlet 拦截。DispatcherServlet 是整个流程的入口点,负责接收所有的请求并调度给合适的处理器。
public class DispatcherServlet extends FrameworkServlet {
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = this.checkMultipart(request);
multipartRequestParsed = processedRequest != request;
mappedHandler = this.getHandler(processedRequest);
if(mappedHandler == null || mappedHandler.getHandler() == null){
this.noHandlerFound(processedRequest, response);
return;
}
...
}
...
}
} finally {
...
}
}
}
mappedHandler = this.getHandler(processedRequest);
HandlerMapping 处理器映射:
DispatcherServlet 会根据请求的 URL 调用 HandlerMapping 来查找对应的处理器(Controller)。HandlerMapping 会返回一个 HandlerExecutionChain 对象,其中包含了目标处理器以及该处理器执行前后的拦截器。
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
Iterator var2 = this.handlerMappings.iterator();
HandlerExecutionChain handler;
do {
...
} while(handler == null);
return handler;
}
HandlerAdapter 处理器适配器调用处理器:
找到处理器后,DispatcherServlet 会通过 HandlerAdapter 调用实际的处理器(Controller)方法。不同的处理器可能需要不同的适配器来执行,因此 HandlerAdapter 起到了适配的作用。
public class DispatcherServlet extends FrameworkServlet {
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = this.checkMultipart(request);
multipartRequestParsed = processedRequest != request;
mappedHandler = this.getHandler(processedRequest);
if(mappedHandler == null || mappedHandler.getHandler() == null){
this.noHandlerFound(processedRequest, response);
return;
}
HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
...
}
...
}
} finally {
...
}
}
}
HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
Iterator var2 = this.handlerAdapters.iterator();
HandlerAdapter ha;
do {
if (!var2.hasNext()) {
throw new ServletException("No adapter for handler [" + handler + "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
}
ha = (HandlerAdapter)var2.next();
if (this.logger.isTraceEnabled()) {
this.logger.trace("Testing handler adapter [" + ha + "]");
}
} while(!ha.supports(handler));
return ha;
}
public interface HandlerAdapter {
boolean supports(Object var1);
ModelAndView handle(HttpServletRequest var1, HttpServletResponse var2, Object var3) throws Exception;
long getLastModified(HttpServletRequest var1, Object var2);
}
执行处理器(Controller):
在 Controller 中,业务逻辑将被执行。这可能涉及到与服务层、数据访问层的交互以完成业务需求。Controller 方法可能会返回一个 ModelAndView 对象,这个对象包含了视图名和模型数据。
视图解析器 ViewResolver 解析视图:
DispatcherServlet 会使用 ViewResolver 将逻辑视图名转换为具体的视图对象(如 JSP 文件)。如果需要,它还会将模型数据传递给视图。
渲染视图:
视图对象负责渲染输出,即将模型数据转换成 HTML 或其他格式的内容,以便于浏览器显示。
响应客户端:
- 最终,由 DispatcherServlet 将渲染好的结果发送回客户端。
在整个过程中,Spring MVC 还支持各种拦截器(Interceptor),这些拦截器可以在请求到达处理器之前或处理器处理完请求之后执行特定的逻辑,例如权限检查、日志记录等。