入口dispatchservlet
adapter分为三种 HttpRequestHandlerAdapter,SimpleControllerHandlerAdapter和AnnotationMethodHandlerAdapter(注解)
在web.xml声明的 annotation...handler annotation...handleradapter
对应的是是handler 按照适配器模式 应该是handler对应handleradapter 安卓上也用到了适配器模式
listview上面的要把数据修饰成adapter 然后才能付给listview处理。
//dispatchservlet 根据具体的handler找到匹配的handleradapter(适配器模式)
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
//调用handle方法 吧handler传入
HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
if (interceptors != null) {
for (int i = 0; i < interceptors.length; i++) {
//执行拦截器pre
HandlerInterceptor interceptor = interceptors[i];
if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) {
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);
return;
}
interceptorIndex = i;
}
}
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
//handle方法中 吧Handler转化为controller
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
return ((Controller) handler).handleRequest(request, response);
}
//拦截器执行完controller的时候 在执行post拦截器。逆序
if (interceptors != null) {
for (int i = interceptors.length - 1; i >= 0; i--) {
HandlerInterceptor interceptor = interceptors[i];
interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);
}
}
//渲染
render(mv, processedRequest, response);
//handleRequest中根据request的url 匹配到methodname 然后利用反射 调用方法。 返回modelandview(map封装了数据)或者string通过convetor(fastjsonconvetor)封装成json数据
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
String methodName = this.methodNameResolver.getHandlerMethodName(request);
return invokeNamedMethod(methodName, request, response);
}
catch (NoSuchRequestHandlingMethodException ex) {
return handleNoSuchRequestHandlingMethod(ex, request, response);
}
}
//把requestmapping上的url和对应的方法装入到map中
private void registerHandlerMethods(Object delegate) {
this.handlerMethodMap.clear();
this.lastModifiedMethodMap.clear();
this.exceptionHandlerMap.clear();
// Look at all methods in the subclass, trying to find
// methods that are validators according to our criteria
Method[] methods = delegate.getClass().getMethods();
for (Method method : methods) {
// We're looking for methods with given parameters.
if (isExceptionHandlerMethod(method)) {
registerExceptionHandlerMethod(method);
}
else if (isHandlerMethod(method)) {
registerHandlerMethod(method);
registerLastModifiedMethodIfExists(delegate, method);
}
}
}
拦截器
继承Interceptorhandler
prehandle
posthandle
aftercompletion
resetcontroller
@RestController注解相当于@ResponseBody + @Controller合在一起的作用
ResponseBody 是吧返回的字符串或者类(转化为json) 直接放到response的响应体