当前位置:网站首页>十、视图解析原理与源码分析
十、视图解析原理与源码分析
2022-08-05 05:16:00 【呆比特】
视图解析原理与源码分析
前边已经学习完了SpringBoot请求如何处理、参数如何解析,响应如何处理,如何处理返回值。接下来这篇,主要通过源码,逐步分析SpringBoot中的视图解析原理。
我们知道,所有的请求都是从 DispatcherServlet 开始的,现在来到这个类的 doDispatch 方法加上断点,来分析一下页面的跳转过程。
@PostMapping("/login")
public String main(User user, HttpSession session, Model model){
if(StringUtils.hasLength(user.getUserName()) && "123456".equals(user.getPassword())){
//把登陆成功的用户保存起来
session.setAttribute("loginUser",user);
//登录成功重定向到main.html; 重定向防止表单重复提交
return "redirect:/main.html";
}else {
model.addAttribute("msg","账号密码错误");
//回到登录页面
return "login";
}
}
@GetMapping("/main.html")
public String mainPage(HttpSession session,Model model){
return "main";
}
首先,来到登录页输入正确的密码进行登录,请求来到 doDispatch 方法,大概过一下之前学过的流程
下一步
下一步
step into 进入方法,到 RequestMappingHandlerAdapter.class 的 invokeHandlerMethod 执行目标方法
一直下一步,让执行目标方法


下一步,让目标方法执行完,来到 ServletInvocableHandlerMethod.class 的 invokeAndHandle 方法
继续往下走,解析返回值,找到处理我们返回值的处理器 ViewNameMethodReturnValueHandler

继续往下,进入 ViewNameMethodReturnValueHandler 的 handleReturnValue,在目标方法处理的过程中,所有数据和视图地址,都会被放在 ModelAndViewContainer 里面。

方法执行完以后所有的东西就都在 ModelAndViewContainer 里边了,如果方法的参数是一个自定义类型对象(从请求参数中确定的),也会把他重新放在 ModelAndViewContainer

继续下一步让方法执行完,可以发现,任何目标方法执行完成以后都会返回 ModelAndView 保存了数据和视图地址

而且,就算你方法没有返回值,它也会给你默认一个跳转页,比如我们没有返回值,它就会拿到原生的request,查看当前的请求是什么,然后当做跳转的页面

到现在为止,我们的页面还没有跳转,继续往下走,来到一个 processDispatchResult 方法,翻译的话就是处理派发结果,就是它来决定页面如何响应的
进入这个方法,开始视图解析原理

进入方法
View对象定义了页面的渲染逻辑,我们来看看View对象是如何获取的
进入 resolveViewName 方法
我们的返回值为 redirect:/main.html ,所以最终拿到了一个 RedirectView ,它的效果其实就是 重定向到一个页面
视图解析器只有一个作用,就是获取View对象
得到视图对象之后,视图对象会调用它的 render 方法
进入 renderMergedOutputModel ,真正的逻辑方法
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws IOException {
//第一步,获取目标url地址
String targetUrl = this.createTargetUrl(model, request);
targetUrl = this.updateTargetUrl(targetUrl, model, request, response);
RequestContextUtils.saveOutputFlashMap(targetUrl, request, response);
//重定向
this.sendRedirect(request, response, targetUrl, this.http10Compatible);
}

最终就是调用了我们使用servlet的最原始的方法
总结视图解析
返回值以 forward: 开始: new InternalResourceView(forwardUrl); --> 转发
request.getRequestDispatcher(path).forward(request, response);
返回值以 redirect: 开始: new RedirectView() --> render就是重定向
返回值是普通字符串: new ThymeleafView()
OVER(∩_∩)~
边栏推荐
- tensorflow的session和内存溢出
- The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time
- [Go through 11] Random Forest and Feature Engineering
- 读论文 - Unpaired Portrait Drawing Generation via Asymmetric Cycle Mapping
- 如何组织一场安全、可靠、高效的网络实战攻防演习?
- MySql之索引
- Flink Distributed Cache 分布式缓存
- Thread handler句柄 IntentServvice handlerThread
- 记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!
- Flink和Spark中文乱码问题
猜你喜欢
![[Pytorch study notes] 11. Take a subset of the Dataset and shuffle the order of the Dataset (using Subset, random_split)](/img/59/ce3e18f32c40a97631f5ac1b53662a.png)
[Pytorch study notes] 11. Take a subset of the Dataset and shuffle the order of the Dataset (using Subset, random_split)

单变量线性回归

CVPR 2020 - 频谱正则化
![[Over 17] Pytorch rewrites keras](/img/a2/7f0c7eebd119373bf20c44de9f7947.png)
[Over 17] Pytorch rewrites keras

【论文阅读-表情捕捉】ExpNet: Landmark-Free, Deep, 3D Facial Expressions
![[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)](/img/71/f82e76085f9d8e6610f6f817e2148f.png)
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)

【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)

解决:Unknown column ‘id‘ in ‘where clause‘ 问题

6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!

MSRA proposes extreme masking model ExtreMA for learning instances and distributed visual representations
随机推荐
SharedPreferences and SQlite database
SparkML-初探-文本分类
OSPF故障排除办法
服务网格istio 1.12.x安装
华科提出首个用于伪装实例分割的一阶段框架OSFormer
Flink Table API 和 SQL之概述
[Database and SQL study notes] 8. Views in SQL
通过Flink-Sql将Kafka数据写入HDFS
WCH系列芯片CoreMark跑分
发顶会顶刊论文,你应该这样写作
读论文-Cycle GAN
Day1:用原生JS把你的设备变成一台架子鼓!
flink项目开发-flink的scala shell命令行交互模式开发
Flink HA配置
网管日记:故障网络交换机快速替换方法
Tensorflow2 与 Pytorch 在张量Tensor基础操作方面的对比整理汇总
神经网络也能像人类利用外围视觉一样观察图像
基于Flink CDC实现实时数据采集(四)-Sink接口实现
【数据库和SQL学习笔记】8.SQL中的视图(view)
Flink Distributed Cache 分布式缓存