Spring MVC的工作原理和机制
Spring MVC的工作原理和机制
参考:
springMVC 的工作原理和机制 - 孤鸿子 - 博客园
https://www.cnblogs.com/zbf1214/p/5265117.html
工作原理
上面的是springMVC的工作原理图:
1、客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServlet的请求映射路径(在web.xml中指定),web容器将请求转交给DispatcherServlet.
2、DipatcherServlet接收到这个请求之后将根据请求的信息(包括URL、Http方法、请求报文头和请求参数Cookie等)以及HandlerMapping的配置找到处理请求的处理器(Handler)。
3-4、DispatcherServlet根据HandlerMapping找到对应的Handler,将处理权交给Handler(Handler将具体的处理进行封装),再由具体的HandlerAdapter对Handler进行具体的调用。
5、Handler对数据处理完成以后将返回一个ModelAndView()对象给DispatcherServlet。
6、Handler返回的ModelAndView()只是一个逻辑视图并不是一个正式的视图,DispatcherSevlet通过ViewResolver将逻辑视图转化为真正的视图View。
7、Dispatcher通过model解析出ModelAndView()中的参数进行解析最终展现出完整的view并返回给客户端。
工作机制是什么
Control的调用(续)
接着对于(二)的补充:主要是小结下Control的处理逻辑的关键操作;
对于control的处理关键就是:DispatcherServlet的handlerMappings集合中根据请求的URL匹配每一个handlerMapping对象中的某个handler,匹配成功之后将会返回这个handler的处理连接handlerExecutionChain对象。而这个handlerExecutionChain对象中将会包含用户自定义的多个handlerInterceptor对象。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/** * Return the HandlerExecutionChain for this request. * <p>Tries all handler mappings in order. * @param request current HTTP request * @return the HandlerExecutionChain, or <code>null</code> if no handler could be found */ protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { for (HandlerMapping hm : this.handlerMappings) { if (logger.isTraceEnabled()) { logger.trace( "Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'"); } HandlerExecutionChain handler = hm.getHandler(request); if (handler != null) { return handler; } } return null; } |
而对于handlerInterceptor接口中定义的三个方法中,preHandler和postHandler分别在handler的执行前和执行后执行,afterCompletion在view渲染完成、在DispatcherServlet返回之前执行。
PS:这么我们需要注意的是:当preHandler返回false时,当前的请求将在执行完afterCompletion后直接返回,handler也将不会执行。
在类HandlerExecutionChain中的getHandler()方法是返回object对象的;
|
1
2
3
4
5
6
7
|
/** * Return the handler object to execute. * @return the handler object */ public Object getHandler() { return this.handler; } |
这里的handler是没有类型的,handler的类型是由handlerAdapter决定的。dispatcherServlet会根据handler对象在其handlerAdapters集合中匹配哪个HandlerAdapter实例支持该对象。接下来去执行handler对象的相应方法了,如果该handler对象的相应方法返回一个ModelAndView对象接下来就是去执行View渲染了。
|
1
2
3
4
5
6
7
|
/** * Return the handler object to execute. * @return the handler object */ public Object getHandler() { return this.handler; } |
---------------------------------------邪恶的分割线---------------------------------------------
Model设计
如果handler兑现返回了ModelAndView对象,那么说明Handler需要传一个Model实例给view去渲染模版。除了渲染页面需要model实例,在业务逻辑层通常也有Model实例。
ModelAndView对象是连接业务逻辑层与view展示层的桥梁,对spring MVC来说它也是连接Handler与view的桥梁。ModelAndView对象顾名思义会持有一个ModelMap对象和一个View对象或者View的名称。ModelMap对象就是执行模版渲染时候所需要的变量对应的实例,如jsp的通过request.getAttribute(String)获取的JSTL标签名对应的对象。velocity中context.get(String)获取$foo对应的变量实例。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class ModelAndView {/** View instance or view name String */ private Object view; /** Model Map */ private ModelMap model; /** Indicates whether or not this instance has been cleared with a call to {@link #clear()} */ private boolean cleared = false;.....} |
ModelMap其实也是一个Map,Handler中将模版中需要的对象存在这个Map中,然后传递到view对应的ViewResolver中。
|
1
2
3
4
|
public interface ViewResolver { View resolveViewName(String viewName, Locale locale) throws Exception;} |
不同的ViewResolver会对这个Map中的对象有不同的处理方式;
- velocity中将这个Map保存到VelocityContext中。
- JSP中将每一个ModelMap中的元素分别设置到request.setAttribute(modelName,modelValue);
-----------------------邪恶的分割线-----------------------------------------------
view设计
在spring MVC中,view模块需要两个组件来支持:RequestToViewNameTranslator和ViewResolver
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public interface RequestToViewNameTranslator { /** * Translate the given {@link HttpServletRequest} into a view name. * @param request the incoming {@link HttpServletRequest} providing * the context from which a view name is to be resolved * @return the view name (or <code>null</code> if no default found) * @throws Exception if view name translation fails */ String getViewName(HttpServletRequest request) throws Exception;} |
对于 ViewResolver,前面有写到了,就不写了;
-----------------------邪恶的分割线-------------------------------------------------
RequestToViewNameTranslator:主要支持用户自定义对viewName的解析,如将请求的ViewName加上前缀或者后缀,或者替换成特定的字符串等。
ViewResolver:主要是根据用户请求的viewName创建适合的模版引擎来渲染最终的页面,ViewResolver会根据viewName创建一个view对象,调用view对象的Void render方法渲染出页面;
|
1
2
3
|
public interface View {void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception;} |
下面来总结下 Spring MVC解析View的逻辑:
- dispatcherServlet方法调用getDefaultViewName()方法;
|
1
2
3
4
5
6
7
8
9
|
/** * Translate the supplied request into a default view name. * @param request current HTTP servlet request * @return the view name (or <code>null</code> if no default found) * @throws Exception if view name translation failed */ protected String getDefaultViewName(HttpServletRequest request) throws Exception { return this.viewNameTranslator.getViewName(request); } |
- 调用了RequestToViewNameTranslator的getViewName方法;
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public interface RequestToViewNameTranslator { /** * Translate the given {@link HttpServletRequest} into a view name. * @param request the incoming {@link HttpServletRequest} providing * the context from which a view name is to be resolved * @return the view name (or <code>null</code> if no default found) * @throws Exception if view name translation fails */ String getViewName(HttpServletRequest request) throws Exception;} |
- 调用LocaleResolver接口的resolveLocale方法;
|
1
|
Locale resolveLocale(HttpServletRequest request); |
- 调用ViewResolver接口的resolveViewName方法,返回view对象
|
1
|
View resolveViewName(String viewName, Locale locale) throws Exception; |
- 调用render方法渲染出页面
Spring MVC的工作原理和机制的更多相关文章
- Spring MVC的工作原理,我们来看看其源码实现
前言 开心一刻 晚上陪老丈人吃饭,突然手机响了,我手贱按了免提……哥们:快出来喝酒!哥几个都在呢!我:今天不行,我现在陪老丈人吃饭呢.哥们:那你抓紧喝,我三杯白酒,把我岳父放倒了才出来的,你也快点.看 ...
- spring mvc的工作原理
该文转载自:http://blog.csdn.net/u012191627/article/details/41943393 SpringMVC框架介绍 1) spring MVC属于SpringFr ...
- 阿里P7工作总结:Spring MVC的工作原理,看完受益匪浅
这篇文章将深入探讨Spring框架的一部分——Spring Web MVC的强大功能及其内部工作原理. 项目安装 在本文中,我们将使用最新.最好的Spring Framework 5.我们将重点介绍S ...
- Spring MVC 的工作原理
引自:https://www.cnblogs.com/xiaoxi/p/6164383.html SpringMVC的工作原理图: SpringMVC流程 1. 用户发送请求至前端控制器Dispat ...
- Hibernate、Spring和Struts2工作原理
Hibernate.Spring和Struts2工作原理 博客分类: Java 基础 工作HibernateSpringMVCStruts Hibernate.Spring和Struts2工作原理 ...
- Spring Bean的生命周期、Spring MVC的工作流程、IOC,AOP
1.Spring Bean的生命周期? (1)构造方法实例化bean. (2)构造方法设置对象属性. (3)是否实现aware接口,三种接口(BeanNameAware,BeanFactoryAwar ...
- [JavaEE,MVC] Struts工作原理
基本概念 Struts是Apache 基金会Jakarta 项目组的一个Open Source 项目,它采用MVC模式,能够很好地帮助java 开发者利用J2EE开发Web应用.和其他的java架构一 ...
- Spring MVC内容协商实现原理及自定义配置【享学Spring MVC】
每篇一句 在绝对力量面前,一切技巧都是浮云 前言 上文 介绍了Http内容协商的一些概念,以及Spring MVC内置的4种协商方式使用介绍.本文主要针对Spring MVC内容协商方式:从步骤.原理 ...
- Spring MVC的工作机制
1. Spring MVC请所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责负责对请求进行真正的处理工作. 2. DispatcherServlet查询一个或多个H ...
随机推荐
- [转载]Axis2 and CXF的比较
在Celtix 和XFire 宣布合并的同年,另一个著名开源Web 服务框架Axis 的后继者Axis2 也诞生了.Axis2 并非Axis 的2.0 版,而是完全重写了Axis 的新项目.作为功能和 ...
- 定时器:Timer:System.Threading.Timer类(转)
最近的一个项目有一些地方需要用到定时功能,在设计过程中,突然发现.net的Timer类居然还有很多我以前没有用过的功能,这里就跟大家分享一下 注:这里的Timer类特指System.Threading ...
- 4Sum_leetCode
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- Iterator模式----一个一个遍历
说起遍历,我立马就想到for循环,增强for循环,foreach循环这类的循环遍历,这个不错,既然有这么方便的遍历,为什么我们还要学习Iterator这样的遍历呢? 一个重要的理由是:引入Iterat ...
- org.hibernate.type.SerializationException: could not deserialize 反序列化失败
1.查看实体类有没有实现Serializable接口 例:public class Student implements Serializable { ***** } 2.看表中的字段有没有在实体中进 ...
- python之开篇---hello world!
(1)前沿 (2)python 简介 (3)python hello world 实现 (4) -------------qq:1327706646 ------------------------- ...
- php != 和 !== 的区别
== and != do not take into account the data type of the variables you compare. So these would all re ...
- vue-router实现页面的整体跳转
直接看效果图: 代码地址:https://github.com/YalongYan/vue-router-jump
- 【很强大的Android图表引擎 - AChartSDK】
在手机移动App开发中,图表在app中越来越占领举足轻重的地图.而在Android领域.AchartEngine 图表引擎可谓无人不知无人不晓. 可是今天就给各位推荐更为强大的图表引擎. 为什么说更为 ...
- poj2349
Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 0 Accepted: 0 Descript ...