前言
SparingMVC完成了请求接收、获取HandleMapping、在通过HandelMapping获得HandlerAdapter,应用前置请求处理器,执行具体逻辑,获取试图名称、应用后置处理器、处理请求分发结果:完成试图解析并返回。
一、DispatcherServlet(调度程序)
- 初始化策略:初始化响应的解释器或适配器
- 请求处理:从请求接受到请求处理结束。
类实现名称
public class DispatcherServlet extends FrameworkServlet
初始化策略(initStrategies)
在父类FrameworkServlet类中方法调用
//刷新初始化策略
protected void onRefresh(ApplicationContext context) {
this.initStrategies(context);
}
//初始化策略,如果没有设置配置,根据默认配置即DispatcherServlet.properties来设置
protected void initStrategies(ApplicationContext context) {
//初始化多文件处理器 LocaleResolver
this.initMultipartResolver(context);
//初始化本地资源处理器 LocaleResolver
this.initLocaleResolver(context);
//初始化主提处理器 ThemeResolver
this.initThemeResolver(context);
//初始化处理器映射器 HandlerMapping
this.initHandlerMappings(context);
//初始化处理器适配器 HandlerAdapter
this.initHandlerAdapters(context);
//初始化处理器异常处理器 HandlerExceptionResolver
this.initHandlerExceptionResolvers(context);
//初始化请求解析试图名称翻译 RequestToViewNameTranslator
this.initRequestToViewNameTranslator(context);
//初始化试图处理器 ViewResolver
this.initViewResolvers(context);
//初始化显示映射管理 FlashMapMager
this.initFlashMapManager(context);
}
处理请求
通过doService设置请求需要相关资源。在调用doDispatch具体执行调度。
执行调度过程:
- 获得Web异步管理器(WebAsyncManager)
- 判断是否多文件如果是多文件返回多文件请求,否则返回当前request
- 根据request,获取映射处理器。(MapperHandle)
- 通过映射处理器中的处理器获得处理器适配器。 (HandleAdapt)
- 应用前置处理
- 处理器适配器质性处理,获得模型和试图(ModeAndView)
- 应用后置处理
- 分发结果
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
this.logRequest(request);
Map<String, Object> attributesSnapshot = null;
//判断请求属性中是否包含javax.servlet.include.request_uri
if (WebUtils.isIncludeRequest(request)) {
attributesSnapshot = new HashMap();
//获取所有请求属性
Enumeration attrNames = request.getAttributeNames();
label95:
while(true) {
String attrName;
do {
if (!attrNames.hasMoreElements()) {
break label95;
}
attrName = (String)attrNames.nextElement();
} while(!this.cleanupAfterInclude && !attrName.startsWith("org.springframework.web.servlet"));
attributesSnapshot.put(attrName, request.getAttribute(attrName));
}
}
//设置请求需要使用到的资源,具体可以看初始化
request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.getWebApplicationContext());
request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);
request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);
request.setAttribute(THEME_SOURCE_ATTRIBUTE, this.getThemeSource());
if (this.flashMapManager != null) {
FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(request, response);
if (inputFlashMap != null) {
request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap));
}
request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager);
}
try {
//执行调度
this.doDispatch(request, response);
} finally {
if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted() && attributesSnapshot != null) {
this.restoreAttributesAfterInclude(request, attributesSnapshot);
}
}
}
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;
Object dispatchException = null;
try {
//判断是否多文件
processedRequest = this.checkMultipart(request);
multipartRequestParsed = processedRequest != request;
//获取映射处理器
mappedHandler = this.getHandler(processedRequest);
if (mappedHandler == null) {
this.noHandlerFound(processedRequest, response);
return;
}
//获取处理器适配器
HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if ((new ServletWebRequest(request, response)).checkNotModified(lastModified) && isGet) {
return;
}
}
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
//执行处理,获取模型和试图
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
//应用默认试图名称
this.applyDefaultViewName(processedRequest, mv);
//应用前置处理
mappedHandler.applyPostHandle(processedRequest, response, mv);
} catch (Exception var20) {
dispatchException = var20;
} catch (Throwable var21) {
dispatchException = new NestedServletException("Handler dispatch failed", var21);
}
//流程调度结果
this.processDispatchResult(processedRequest, response, mappedHandler, mv, (Exception)dispatchException);
} catch (Exception var22) {
this.triggerAfterCompletion(processedRequest, response, mappedHandler, var22);
} catch (Throwable var23) {
this.triggerAfterCompletion(processedRequest, response, mappedHandler, new NestedServletException("Handler processing failed", var23));
}
} finally {
if (asyncManager.isConcurrentHandlingStarted()) {
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
} else if (multipartRequestParsed) {
this.cleanupMultipart(processedRequest);
}
}
}
二、使用步骤
1.引入库
代码如下(示例):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
2.读入数据
代码如下(示例):
data = pd.read_csv(
'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())
该处使用的url网络请求的数据。
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。