SpringMvc如何将Url 映射到 RequestMapping (一)
SpringMvc Url 匹配规则详解
最近开始阅读Spring 源码,虽然用了很久的spring ,但是没有真正的分析过Spring时如何工作的。今天重 MVC 的Url匹配规则开始进行Spring源码的阅读。
一、Springmvc url 匹配规则
RequestMapping中路径的定义
1: /abc 指定具体的定义值
2:/{type} 指定参数 即 /###
3:/** 匹配任何值 /###/### 可以匹配任意数量
4:/abc/*/abc 匹配中间固定值 /abc/###/abc
二、源码分析
private static class PatternInfo {
private final String pattern; // 匹配的url规则 例如 /abc/{type}
private int uriVars; // url 参数
private int singleWildcards; // 单个*匹配符
private int doubleWildcards; // ** 匹配符
private boolean catchAllPattern; // 是否完全匹配
private boolean prefixPattern; // 是否前缀匹配
private Integer length; // 匹配条件长度
public PatternInfo(String pattern) {
this.pattern = pattern;
if (this.pattern != null) {
initCounters();
this.catchAllPattern = this.pattern.equals("/**");
this.prefixPattern = !this.catchAllPattern && this.pattern.endsWith("/**");
}
if (this.uriVars == 0) {
this.length = (this.pattern != null ? this.pattern.length() : 0);
}
}
protected void initCounters() {
int pos = 0;
while (pos < this.pattern.length()) {
if (this.pattern.charAt(pos) == '{') {
this.uriVars++;
pos++;
}
else if (this.pattern.charAt(pos) == '*') {
if (pos + 1 < this.pattern.length() && this.pattern.charAt(pos + 1) == '*') {
this.doubleWildcards++;
pos += 2;
}
else if (pos > 0 && !this.pattern.substring(pos - 1).equals(".*")) {
this.singleWildcards++;
pos++;
}
else {
pos++;
}
}
else {
pos++;
}
}
}
public int getUriVars() {
return this.uriVars;
}
public int getSingleWildcards() {
return this.singleWildcards;
}
public int getDoubleWildcards() {
return this.doubleWildcards;
}
public boolean isLeastSpecific() {
return (this.pattern == null || this.catchAllPattern);
}
public boolean isPrefixPattern() {
return this.prefixPattern;
}
public int getTotalCount() {
return this.uriVars + this.singleWildcards + (2 * this.doubleWildcards);
}
/**
* Returns the length of the given pattern, where template variables are considered to be 1 long.
*/
public int getLength() {
if (this.length == null) {
this.length = VARIABLE_PATTERN.matcher(this.pattern).replaceAll("#").length();
}
return this.length;
}
}
/* Pattern Url正则表达式排序规则 */
protected static class AntPatternComparator implements Comparator<String> {
/** 将所有匹配的URL 进行排序 */
public int compare(String pattern1, String pattern2) {
PatternInfo info1 = new PatternInfo(pattern1);
PatternInfo info2 = new PatternInfo(pattern2);
// 规则1
if (info1.isLeastSpecific() && info2.isLeastSpecific()) {
// 如果 p1 和 p2 都是 /** 则两个地址相同
return 0;
}
else if (info1.isLeastSpecific()) {
// 如果 p1 是 /** 则p2优先
return 1;
}
else if (info2.isLeastSpecific()) {
// 反之 p2 是 /** 则p1优先
return -1;
}
// 规则2
boolean pattern1EqualsPath = pattern1.equals(path);
boolean pattern2EqualsPath = pattern2.equals(path);
if (pattern1EqualsPath && pattern2EqualsPath) {
// 请求url 和p1 、p2 完全相等
return 0;
}
else if (pattern1EqualsPath) {
// 请求url 和 p1 相等 则 p1 优先
return -1;
}
else if (pattern2EqualsPath) {
// 请求url 和 p2 相等 则p2 优先
return 1;
}
// 规则3
if (info1.isPrefixPattern() && info2.getDoubleWildcards() == 0) {
// 如果p1 是 前缀匹配 且 p2 不包含 /** 则 p2 优先
return 1;
}
else if (info2.isPrefixPattern() && info1.getDoubleWildcards() == 0) {
//如果p2 是 前缀匹配 且 p1 不包含 /** 则 p1 优先
return -1;
}
// 规则4
if (info1.getTotalCount() != info2.getTotalCount()) {
// getTotalCount = this.uriVars + this.singleWildcards + (2 * this.doubleWildcards);
// 参数匹配个数 + 单个匹配个数 + 多个匹配个数
return info1.getTotalCount() - info2.getTotalCount();
}
// 规则5
if (info1.getLength() != info2.getLength()) {
// 正则匹配个数越多 则优先级越高
return info2.getLength() - info1.getLength();
}
// 规则6
if (info1.getSingleWildcards() < info2.getSingleWildcards()) {
return -1;
}
else if (info2.getSingleWildcards() < info1.getSingleWildcards()) {
return 1;
}
// 规则7
if (info1.getUriVars() < info2.getUriVars()) {
return -1;
}
else if (info2.getUriVars() < info1.getUriVars()) {
return 1;
} return 0;
}
根据上面
规则1: /** 匹配优先级最低 即当请求url其他所有都不能匹配时 则匹配 /**
规则2:路径全匹配优先级最高
规则3:前缀匹配 和 包含/** 匹配 优先级较低
规则4:参数匹配、单个匹配、多个匹配 匹配规则数量越少 则优先级越高
规则5:正则匹配个数越多,则优先级越高 区别于4 这里是和 url进行匹配后进行比较,4中单指 规则中的匹配规则数量
规则6:单匹配 规则少则优先级高
规则7:参数匹配规则少则优先级高
综合上面7个规则
总结如下:
/** 、 abc/** 规则匹配度较低
/abc/def 匹配度最高
/{type}/def 匹配度 高于 /{type}/{name}
/{} /* /** 出现越少 匹配度越高
/{} 优先级 高于 /*
/* 优先级 高于 /**
url:/test/d_d_d/test 进行匹配 则 /test/d_d_d/test > /test/{type}/test > /test/*/test > /test/{type}_{type}/test > /test/**/test > /test/{type}_{type}_{type}/test > /test/** > /**
SpringMvc如何将Url 映射到 RequestMapping (一)的更多相关文章
- SpringMvc如何将Url 映射到 RequestMapping (二)
昨天简单分析了Springmvc 中 RequestMapping 配置的url和请求url之间的匹配规则.今天详细的跟踪一下一个请求url如何映射到Controller的对应方法上 一.入口 org ...
- SpringMVC中url映射到Controller
SpringMVC也是一种基于请求驱动的WEB框架,并且使用了前端控制器的设计模式.前端控制器就是DispatcherServlet控制器,只要满足web.xml文件中的[url-pattern]的规 ...
- SpringMvc的Url映射和传参案例(转)
Springmvc的基本使用,包括url映射.参数映射.页面跳转.ajax和文件上传 以前学习的时候写的代码案例,今天整理笔记的时候找到了,很久没有来园子了,发上来当个在线笔记用吧,免的时间长了又忘了 ...
- SpringMvc的Url映射和传参案例
Springmvc的基本使用,包括url映射.参数映射.页面跳转.ajax和文件上传 以前学习的时候写的代码案例,今天整理笔记的时候找到了,很久没有来园子了,发上来当个在线笔记用吧,免的时间长了又忘了 ...
- 关于requestMapping 进行url映射实现小小知识点 以及如何获取请求的url中的参数
requstMapping 用来处理url映射 可以作用在controller类上 也可以作用在方法上 经常使用的方式 通过接收一种映射关系 @RequestMapping("/del ...
- 应用springMVC时如果配置URL映射时如下配置
应用springMVC时如果配置URL映射时如下配置 [html] view plaincopy<servlet> <servlet-name>appServlet</s ...
- SpringMVC基础03——常用注解之@RequestMapping
1.用法 SpringMVC使用@RequestMapping注解,为控制器指定可以处理哪些URL请求,并且可以指定处理请求的类型(POST/GET),如果@RequestMapping没有指定请求的 ...
- SpringMVC的HandlerMapping(处理器映射器)
SpringMvc有四种HandlerMapping:BeanNameUrlHandlerMapping ,SimpleUrlHandlerMapping,ControllerClassNameHan ...
- springMVC三种处理器映射器
1.配置处理器映射器,springmvc默认的处理器映射器BeanNameUrlHandlerMapping <bean class="org.springframework.web. ...
随机推荐
- 【转载】SOAP协议介绍
SOAP是用在分散或分布的环境中交换信息的简单的协议,它是一个基于XML的协议,包括三个部分:封装定义了一个描述消息中包含什么内容以及如何处理它们的框架,编码规则用于表示应用程序定义的数据类型的实例, ...
- shell操作Hbase
status:查询集群的一些状态 hbase(main):002:0> status1 active master, 0 backup masters, 1 servers, 0 dead, 3 ...
- Python开发【第2节】【Python运算符】
Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员运算符 身份运算符 运算符优先级 1.算术运算符 假设变量a = 10,变量b = 21: ...
- pat1043:输出PATest
https://www.patest.cn/contests/pat-b-practise/1043 #include "stdio.h" int main() { int i, ...
- 【iOS系列】-iOS开发,GET,POST请求使用
[iOS系列]-iOS开发,GET,POST请求使用 步骤: 1:实例化URL(网络资源) 2:根据URL建立URLRequest(网络请求) 默认为GET请求: 对于POST请求,需要创建请求的数据 ...
- /etc/profile与/etc/bashrc、交互式与非交互式、login与non-login shell的差别
线上的memcached又挂了.仍然没有得到core文件. 排查原因,同事发现启动memcached的脚本存在可疑问题. 问题一:没有设置memcached工作文件夹,有可能core dump时没有工 ...
- The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF
打开表单偶尔会出现这个提示,解决方法: web.config增加配置: <configuration> <system.net> <settings> <ht ...
- Educational Codeforces Round 18 C. Divide by Three DP
C. Divide by Three A positive integer number n is written on a blackboard. It consists of not more ...
- 通过fsharp 使用Enterprise Library Unity 3 - 三种拦截模式的探索
这篇就三种拦截模式进行一下探索. 特性总结 类型 特点 其它 InterfaceInterceptor Innstance 仅单接口 类内部函数互相引用无法引起拦截行为 TransparentPr ...
- Django 使用UEditor
Django package 的一些包不支持upload file, 而且 有几个支持的不是收费的就是要开csrf ,这对于苦逼程序猿来说始终是件恼火的事.所以经过查阅各种资料.看了各种各样的配置do ...