springMVC 的工作原理和机制(转)
工作原理
上面的是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方法渲染出页面
springMVC 的工作原理和机制(转)的更多相关文章
- springMVC 的工作原理和机制
工作原理上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServlet的请求映射路径(在web. ...
- 170529、springMVC 的工作原理和机制
工作原理上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServlet的请求映射路径(在web. ...
- Spring MVC的工作原理和机制
Spring MVC的工作原理和机制 参考: springMVC 的工作原理和机制 - 孤鸿子 - 博客园https://www.cnblogs.com/zbf1214/p/5265117.html ...
- SpringMvc @PathVariable 工作原理
SpringMvc @PathVariable 工作原理: 友情提示:查看清晰大图,请鼠标右击图片后,选择新标签页中打开. 相关对象: DispatcherServlet DefaultAnnot ...
- SpringMVC的工作原理及MVC设计模式
SpringMVC的工作原理: 1.当用户在浏览器中点击一个链接或者提交一个表单时,那么就会产生一个请求(request).这个请求会携带用户请求的信息,离开浏览器. 2.这个请求会首先到达Sprin ...
- 复习一下SpringMVC的工作原理
上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServlet的请求映射路径(在web.xml中 ...
- SpringMVC的工作原理(转)
SpringMVC的工作原理图: SpringMVC流程 1. 用户发送请求至前端控制器DispatcherServlet. 2. DispatcherServlet收到请求调用HandlerMa ...
- SpringMVC是怎么工作的,SpringMVC的工作原理
SpringWeb MVC 是怎么工作的,SpringMVC的原理,SpringMVC源码 分析. 介绍 SpringWeb MVC是Spring Framework中的一部分,当我们需要使用spri ...
- SpringMVC的工作原理
1.首先浏览器发送请求给前端控制器DispatcherServlet,DispatcherSerlvet根据请求信息,基于一定的原则选择合适的控制器进行处理并把请求委托给它. 2.页面控制器接收到请求 ...
随机推荐
- codeforces 577B. Modulo Sum 解题报告
题目链接:http://codeforces.com/problemset/problem/577/B 题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中 ...
- codeforces gym 100286 H - Hell on the Markets (贪心算法)
题目链接 题意:n个数分别为a[i],问是否存在一组对应的b[i],b[i]=1 || b[i]=-1,使得ai*bi的n项和为0. 题解: 先证明一个结论吧,对于1≤ai≤i+1,前面ai个数一定可 ...
- Get与Post数据长度的限制
这个问题在我的开发中也遇到,所以在此贴出来(也是在网上搜出来的,呵呵)这是原贴地址http://blog.csdn.net/somat/archive/2004/10/29/158707.aspx两个 ...
- ArtDialog简单使用示例
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- 使用Apache+Dreamweaver(或者H-builder)搭建php开发环境
使用得工具说明 php+Apache服务器+Dreamweaver+mysql数据库 下载安装好wamp,可以在网上直接百度下载,为了方便,我给放个百度云的链接.wamp下载:链接:http://pa ...
- ios wax热更新之安装wax(xcode7.3.1)
通过Finder浏览到你保存该项目的文件夹.创建三个新的文件夹:wax.scripts和Classes. 第一:首先,下载源代码的压缩包.Wax放在GitHub上(https://github.com ...
- August 19th 2016 Week 34th Friday
Friends are not the people you meet at the top, they are the people who were with you at the bottom. ...
- 数独挑战(codevs 2924)
2924 数独挑战 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description “芬兰数学家因卡拉,花费3 ...
- C语言字母频率统计
在进行密码破解时有时候需要得到字母出现的频率信息,下面我将简单的使用C语言来读取一个文件,然后统计该文件内的字母出现的频率. 1.在D盘下新建一个文本文件(文件名为"A.txt") ...
- poj1733(种类并查集+离散化)
题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...