//渲染jsp的流程
DispatcherServlet(HttpServlet).service(HttpServletRequest, HttpServletResponse)
DispatcherServlet(FrameworkServlet).doGet(HttpServletRequest, HttpServletResponse)
DispatcherServlet(FrameworkServlet).processRequest(HttpServletRequest, HttpServletResponse)
DispatcherServlet.doService(HttpServletRequest, HttpServletResponse)
DispatcherServlet.doDispatch(HttpServletRequest, HttpServletResponse)
AnnotationMethodHandlerAdapter.handle(HttpServletRequest, HttpServletResponse,HandlerExecutionChain handler) //mappedHandler.getHandler()
AnnotationMethodHandlerAdapter.invokeHandlerMethod(HttpServletRequest, HttpServletResponse, HandlerExecutionChain handler)
Method.invoke(Object, Object...)
DelegatingMethodAccessorImpl.invoke(Object, Object[])
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) [native method]
HelloWorldController.hello(Model)
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {
protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doService(request, response);
}
protected abstract void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {doDispatch(request, response);}
}
}
public class DispatcherServlet extends FrameworkServlet {
//doDispatch()是DispatcherServlet类 调度的核心
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HandlerExecutionChain mappedHandler = null;
try {
ModelAndView mv = null;
Exception exception = null;
try {
//核心1:查找映射处理器HandlerExecutionChain
mappedHandler = getHandler(request); // Determine handler for the current request.
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(request, response);
return;
}
//核心2:查找处理适配器HandlerAdapter
// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
if (!mappedHandler.applyPreHandle(request, response)) {
return;
}
//调用功能处理器相应的方法,如[public java.util.Map com.demo.springmvc.HelloWorldController.json(org.springframework.ui.Model)]
// Actually invoke the handler.
mv = ha.handle(request, response, mappedHandler.getHandler());
applyDefaultViewName(request, mv);
mappedHandler.applyPostHandle(request, response, mv);
}
catch (Exception ex) {
exception = ex;
}
//处理分发结果并渲染视图
processDispatchResult(request, response, mappedHandler, mv, exception);
}
catch (Exception ex) {
triggerAfterCompletion(request, response, mappedHandler, ex);
}
catch (Error err) {
triggerAfterCompletionWithError(request, response, mappedHandler, err);
}
finally {
}
}
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
HandlerExecutionChain mappedHandler, ModelAndView mv, Exception exception) throws Exception {
boolean errorView = false;
if (exception != null) { //异常处理,
if (exception instanceof ModelAndViewDefiningException) {
logger.debug("ModelAndViewDefiningException encountered", exception);
mv = ((ModelAndViewDefiningException) exception).getModelAndView();
}
else {
Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
mv = processHandlerException(request, response, handler, exception);
}
}
// Did the handler return a view to render?
if (mv != null && !mv.wasCleared()) {
render(mv, request, response);
}
if (mappedHandler != null) {
mappedHandler.triggerAfterCompletion(request, response, null);
}
}
}
package org.springframework.web.servlet;
public class HandlerExecutionChain {
private final Object handler;
//HandlerExecutionChain with handler [public java.util.Map com.demo.springmvc.HelloWorldController.json(org.springframework.ui.Model)] and 1 interceptor
private HandlerInterceptor[] interceptors;
//[org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor@1663921]
private List<HandlerInterceptor> interceptorList;
public Object getHandler() {
return this.handler;
}
public String toString();
}
HandlerExecutionChain
Handler execution chain, consisting of handler object and any handler interceptors.
Returned by HandlerMapping's HandlerMapping.getHandler(javax.servlet.http.HttpServletRequest) method.
Method Summary
void addInterceptor(HandlerInterceptor interceptor)
void addInterceptors(HandlerInterceptor... interceptors)
java.lang.Object getHandler() //Return the handler object to execute.
HandlerInterceptor[] getInterceptors() //Return the array of interceptors to apply (in the given order).
java.lang.String toString() //Delegates to the handler's toString().
package org.springframework.web.servlet;
public interface HandlerAdapter {
boolean supports(Object handler);
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
}
All Known Implementing Classes:
AbstractHandlerMethodAdapter,
AnnotationMethodHandlerAdapter,
HttpRequestHandlerAdapter,
RequestMappingHandlerAdapter,
SimpleControllerHandlerAdapter,
SimpleServletHandlerAdapter
public abstract class AbstractHandlerMethodAdapter extends WebContentGenerator implements HandlerAdapter, Ordered {
//handleInternal是抽象方法
protected abstract ModelAndView handleInternal(HttpServletRequest request,HttpServletResponse response, HandlerMethod handlerMethod) throws Exception;
public final boolean supports(Object handler) {
return (handler instanceof HandlerMethod && supportsInternal((HandlerMethod) handler));
}
public final ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return handleInternal(request, response, (HandlerMethod) handler);
}
}
public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter implements BeanFactoryAware, InitializingBean {
private ModelAndView invokeHandleMethod(HttpServletRequest request,HttpServletResponse response, HandlerMethod handlerMethod) throws Exception
{
requestMappingMethod.invokeAndHandle(webRequest, mavContainer);
return getModelAndView(mavContainer, modelFactory, webRequest);
}
}
public class AnnotationMethodHandlerAdapter extends WebContentGenerator implements HandlerAdapter, Ordered, BeanFactoryAware {
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, HandlerExecutionChain handler) throws Exception {
return invokeHandlerMethod(request, response, handler);
}
protected ModelAndView invokeHandlerMethod(HttpServletRequest request, HttpServletResponse response, HandlerExecutionChain handler) throws Exception {
ServletHandlerMethodResolver methodResolver = getMethodResolver(handler);
Method handlerMethod = methodResolver.resolveHandlerMethod(request);
ServletHandlerMethodInvoker methodInvoker = new ServletHandlerMethodInvoker(methodResolver);
ServletWebRequest webRequest = new ServletWebRequest(request, response);
ExtendedModelMap implicitModel = new BindingAwareModelMap();
Object result = methodInvoker.invokeHandlerMethod(handlerMethod, handler, webRequest, implicitModel); //ModelAndView
ModelAndView mv =
methodInvoker.getModelAndView(handlerMethod, handler.getClass(), result, implicitModel, webRequest);
methodInvoker.updateModelAttributes(handler, (mv != null ? mv.getModel() : null), implicitModel, webRequest);
return mv;
}
}