目标位于 org.apache.tomcat.util.http.mapper.Mapper 中
protectedvoid addWrapper(ContextVersion context, String path,
Object wrapper, booleanjspWildCard, booleanresourceOnly) {
synchronized (context) {
if (path.endsWith("/*")) {
// Wildcard wrapper
//粗略匹配
String name = path.substring(0, path.length() - 2);
Wrapper newWrapper = new Wrapper(name, wrapper, jspWildCard,
resourceOnly);
Wrapper[] oldWrappers = context.wildcardWrappers;
Wrapper[] newWrappers =
new Wrapper[oldWrappers.length + 1];
if (insertMap(oldWrappers, newWrappers, newWrapper)) {
context.wildcardWrappers = newWrappers;
intslashCount = slashCount(newWrapper.name);
if (slashCount > context.nesting) {
context.nesting = slashCount;
}
}
} elseif (path.startsWith("*.")) {
// Extension wrapper
//扩展名匹配
String name = path.substring(2);
Wrapper newWrapper = new Wrapper(name, wrapper, jspWildCard,
resourceOnly);
Wrapper[] oldWrappers = context.extensionWrappers;
Wrapper[] newWrappers =
new Wrapper[oldWrappers.length + 1];
if (insertMap(oldWrappers, newWrappers, newWrapper)) {
context.extensionWrappers = newWrappers;
}
} elseif (path.equals("/")) {
// Default wrapper
//如果配置为/表示将替换tomcat的默认servlet.用于处理所有请求,包括静态资源,这也就是为什么springmvc在配置为 / 时会需要额外处置静态资源。
Wrapper newWrapper = new Wrapper("", wrapper, jspWildCard,
resourceOnly);
context.defaultWrapper = newWrapper;
} else {
// Exact wrapper
//以上均不满足,那么将会被认定为精确匹配,
final String name;
if (path.length() == 0) {
// Special case for the Context Rootmapping which is
// treated as an exact match
name = "/";
} else {
name = path;
}
Wrapper newWrapper = new Wrapper(name, wrapper, jspWildCard,
resourceOnly);
Wrapper[] oldWrappers = context.exactWrappers;
Wrapper[] newWrappers =
new Wrapper[oldWrappers.length + 1];
if (insertMap(oldWrappers, newWrappers, newWrapper)) {
context.exactWrappers = newWrappers;
}
}
}
}
没有什么比源码更容易让人理解内部细节。