当前位置: 首页 > news >正文

深圳产品推广网站建设方案什么是seo教程

深圳产品推广网站建设方案,什么是seo教程,wordpress无刷新分页,企业网站建设的优势前面分析了 request 与 handler method 映射关系的注册,现在再来分析一下 SpringMVC 是如何根据 request 来获取对应的 handler method 的? 可能有人会说,既然已经将 request 与 handler method 映射关系注册保存在了 AbstractHandlerMethodMapping.Ma…

前面分析了 request 与 handler method 映射关系的注册,现在再来分析一下 SpringMVC 是如何根据 request 来获取对应的 handler method 的?

可能有人会说,既然已经将 request 与 handler method 映射关系注册保存在了 AbstractHandlerMethodMapping.MappingRegistry#registry 中,那么根据 request 不就能直接从 registry 中获取到相应的 handler method 了吗?

如果我们定义的 Controller 中的 @RequestMapping 都是普通的字符的话,那确实是可以直接通过 registry 获取 handler method。
但是,@RequestMapping 还支持占位符、通配符等 url,例如:

@RequestMapping("/resources/ima?e.png") // 匹配路径段中的一个字符
@RequestMapping("/resources/*.png") // 匹配路径段中的零个或多个字符
@RequestMapping("/resources/**") // 匹配多个路径段
@RequestMapping("/projects/{project}/versions") // 匹配路径段并将其捕获为变量
@RequestMapping("/projects/{project:[a-z]+}/versions") // 使用正则表达式匹配并捕获变量

所以,查找 request 对应的 handler method 就不是那么简单的事情了。

如何通过 request 获取 handler?

通过对 DispatcherServlet 的分析,我们知道 SpringMVC 获取 handler 的方法如下:
org.springframework.web.servlet.DispatcherServlet#getHandler()

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {if (this.handlerMappings != null) {for (HandlerMapping mapping : this.handlerMappings) {HandlerExecutionChain handler = mapping.getHandler(request);if (handler != null) {return handler;}}}return null;
}

可以看出,SpringMVC 是通过 HandlerMapping#getHandler() 来获取 request 对应的 handler method 处理程序的,最终会拿到一个 HandlerExecutionChain

HandlerExecutionChain 中包含了 request 对应的 handler method 和 interceptors。

HandlerMapping 的类图如下:
HandlerMapping.png

HandlerMapping 的实现类中其中最常用的是 RequestMappingHandlerMapping,它是专门用来处理 @RequestMapping 定义的 request 请求映射的。

获取 HandlerExecutionChain 的过程

HandlerExecutionChain 的获取是在 AbstractHandlerMapping#getHandler() 中完成的。
主要分成了两步:
1、获取 request 对应的 handler 程序
2、将 handler 和 相应的 HandlerInterceptors 组装成 HandlerExecutionChain

getHandler.png

获取 request 对应的 handler method 的过程

上一步获取 HandlerExecutionChain 时,会调用 AbstractHandlerMethodMapping#getHandlerInternal() 来获取 request 对应的 handler method。
最终它会调用到 AbstractHandlerMethodMapping#lookupHandlerMethod() 来获取。

lookupHandlerMethod.png

可以看到,AbstractHandlerMethodMapping#lookupHandlerMethod 实现了 request 与 handler method 映射关系的查找:
1、首先,通过 directPath 直接获取映射的 handler method
2、如果通过 directPath 没有找到的话,就循环 registry 中所有的映射,查出映射关系
3、如果获取到的映射的 handler 个数 > 1 的话,就找出最合适的匹配

DirectPath: 非 patterns 的 path。
非 patterns 的 path 是指 path 的定义中没有 ?、*、{} 的 path

通过 lookupPath 获取 HandlerMethod

AbstractHandlerMethodMapping#lookupHandlerMethod() 在寻找 HandlerMethod 时,有两种逻辑:

  • 1、直接通过 directPath 快速查找
  • 2、循环所有的映射关系,查找 RequestCondition 条件匹配,获取最合适的匹配

直接通过 directPath 快速查找

directPath 是最快速的方式,直接从 map 中获取匹配结果。
directPath 是不包含占位符、模式匹配符的普通路径,所以这种普通路径是最直接,也是效率最高的。

循环所有的映射关系,通过 RequestCondition 条件匹配,获取最合适的匹配

如果通过 directPath 没有找到任何匹配的话,就需要遍历所有注册的映射关系,找出最合适的匹配。


addMatchingMappings(this.mappingRegistry.getRegistrations().keySet(), matches, request);// 这里会循环所有注册的映射关系,将满足 RequestCondition 的映射查找出来,存放到 matches 中
private void addMatchingMappings(Collection<T> mappings, List<Match> matches, HttpServletRequest request) {for (T mapping : mappings) {T match = getMatchingMapping(mapping, request);if (match != null) {matches.add(new Match(match, this.mappingRegistry.getRegistrations().get(mapping)));}}
}

对于 @RequestMapping 注解形式的 url 映射,AbstractHandlerMethodMapping#getMatchingMapping() 最终会调用实现类的方法 RequestMappingInfoHandlerMapping#getMatchingCondition() 来获取匹配。

getMatchingCondition.png

可以看到,Spring 会检查 RequestMappingInfo 是否满足以下 8 个条件:
1、请求方法 RequestMethod 是否匹配,如:GET, POST, PUT, DELETE 等
2、请求参数 params 是否匹配
3、请求头 header 是否匹配
4、consumes Content-Type 是否匹配
5、produces Content-Type 是否匹配
6、处理 directPath 和 通配符的请求映射匹配
7、处理 ant 风格的请求映射匹配
8、处理自定义的请求映射条件匹配

关于 RequestCondition 的详细分析查看 @RequstMapping和RequestCondition

小结

SpringMVC 是通过 HandlerMapping#getHandler() 来获取 request 对应的 handler method 处理程序的。
底层实现是通过 AbstractHandlerMethodMapping#lookupHandlerMethod 来查找 request 对应的 handler method:
1、首先,通过 directPath 直接获取映射的 handler method
2、如果通过 directPath 没有找到的话,就循环 registry 中所有的映射,查出映射关系
3、如果获取到的映射的 handler 个数 > 1 的话,就找出最合适的匹配

对于 url 映射中的通配符、ant 风格的请求,都是通过 RequestCondition 接口来进行处理的。

http://www.hkea.cn/news/92853/

相关文章:

  • 网络设计方案包括哪些深圳百度推广seo公司
  • 亚马逊跨境电商开店站长工具seo综合查询5g
  • 网站怎么做百度快照logo百度快照优化推广
  • 山西网站建设排名seo技术培训山东
  • 日韩系成人影片成首选网站如何优化推广
  • 网站到期续费通知搜索风云排行榜
  • 网站公司说我们做的网站服务器不够用哪个杭州seo好
  • 类似淘宝网站建设费用杭州哪家seo公司好
  • 装修网站怎样做seo专员很难吗
  • 无锡网站外包如何接广告赚钱
  • 英文网站制作 官网淘宝标题优化网站
  • 电力建设网站网络推广网站的方法
  • 如何做网站窗口网站优化网络推广seo
  • 营销型网站建设效果网络营销策划推广方案
  • 专业的网站搭建多少钱网站seo优化价格
  • 广州公司网站设计制作win10优化大师官网
  • 做调查哪个网站比较可靠百度指数查询
  • 怎么在建设厅网站报名广州网站优化服务
  • 怎么用dw做静态网站b站好看的纪录片免费
  • 济南网站建设那家好网站制作公司有哪些
  • 域名和网站名不一样营销公司
  • discuz做电影网站免费网站seo
  • 惠民建设局网站明年2024年有疫情吗
  • 卫龙的网站是谁做的今日的新闻
  • 厚街找人做网站动态网站设计
  • 永春县住房和城乡规划建设局网站太原seo排名优化软件
  • 怎么上网站后台爱站小工具计算器
  • 网页编辑岗位职责seo上海优化
  • 网站做二维码吗做网站的外包公司
  • 郑州市中原区疫情最新消息上海网站营销seo方案