mvc url匹配禁用后缀访问操作 spring( 二 )
关键是PatternsRequestCondition,具体来说是这个方法:AbstractHandlerMethodMapping 的getHandlerInternal:protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {String lookupPath = this.getUrlPathHelper().getLookupPathForRequest(request);if (this.logger.isDebugEnabled()) {this.logger.debug("Looking up handler method for path " + lookupPath);}HandlerMethod handlerMethod = this.lookupHandlerMethod(lookupPath, request);// 这里是关键,它去寻找,找到了就找到了,找不到就不会再去寻找了if (this.logger.isDebugEnabled()) {if (handlerMethod != null) {this.logger.debug("Returning handler method [" + handlerMethod + "]");} else {this.logger.debug("Did not find handler method for [" + lookupPath + "]");}}return handlerMethod != null ? handlerMethod.createWithResolvedBean() : null;}protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {List<AbstractHandlerMethodMapping<T>.Match> matches = new ArrayList();List<T> directPathMatches = (List)this.urlMap.get(lookupPath); // directPathMatches,直接匹配,也可以说是 精确匹配if (directPathMatches != null) {this.addMatchingMappings(directPathMatches, matches, request);// 如果能够精确匹配,就会进来这里}if (matches.isEmpty()) {this.addMatchingMappings(this.handlerMethods.keySet(), matches, request);// 如果无法精确匹配,就会进来这里}if (!matches.isEmpty()) {Comparator<AbstractHandlerMethodMapping<T>.Match> comparator = new AbstractHandlerMethodMapping.MatchComparator(this.getMappingComparator(request));Collections.sort(matches, comparator);if (this.logger.isTraceEnabled()) {this.logger.trace("Found " + matches.size() + " matching mapping(s) for [" + lookupPath + "] : " + matches);}AbstractHandlerMethodMapping<T>.Match bestMatch = (AbstractHandlerMethodMapping.Match)matches.get(0);if (matches.size() > 1) {AbstractHandlerMethodMapping<T>.Match secondBestMatch = (AbstractHandlerMethodMapping.Match)matches.get(1);if (comparator.compare(bestMatch, secondBestMatch) == 0) {Method m1 = bestMatch.handlerMethod.getMethod();Method m2 = secondBestMatch.handlerMethod.getMethod();throw new IllegalStateException("Ambiguous handler methods mapped for HTTP path '" + request.getRequestURL() + "': {" + m1 + ", " + m2 + "}");}}this.handleMatch(bestMatch.mapping, lookupPath, request);return bestMatch.handlerMethod;} else {return this.handleNoMatch(this.handlerMethods.keySet(), lookupPath, request);}}public List<String> getMatchingPatterns(String lookupPath) {List<String> matches = new ArrayList();Iterator var3 = this.patterns.iterator();while(var3.hasNext()) {String pattern = (String)var3.next(); // pattern 是 @RequestMapping 提供的映射String match = this.getMatchingPattern(pattern, lookupPath); //lookupPath + .*后能够匹配pattern,那么就不为空if (match != null) {matches.add(match);// 对于有后缀的情况,.* 后}}Collections.sort(matches, this.pathMatcher.getPatternComparator(lookupPath));return matches;}最关键是这里 getMatchingPatterns :private String getMatchingPattern(String pattern, String lookupPath) {if (pattern.equals(lookupPath)) {return pattern;} else {if (this.useSuffixPatternMatch) {if (!this.fileExtensions.isEmpty() && lookupPath.indexOf(46) != -1) {Iterator var5 = this.fileExtensions.iterator();while(var5.hasNext()) {String extension = (String)var5.next();if (this.pathMatcher.match(pattern + extension, lookupPath)) {return pattern + extension;}}} else {boolean hasSuffix = pattern.indexOf(46) != -1;if (!hasSuffix && this.pathMatcher.match(pattern + ".*", lookupPath)) {return pattern + ".*"; // 关键是这里}}}if (this.pathMatcher.match(pattern, lookupPath)) {return pattern;} else {return this.useTrailingSlashMatch && !pattern.endsWith("/") && this.pathMatcher.match(pattern + "/", lookupPath) ? pattern + "/" : null;}}}
而对于AbstractUrlHandlerMapping,匹配不上就是匹配不上,不会进行 +.* 后在匹配 。
关键方法是这个:protected Object lookupHandler(String urlPath, HttpServletRequest request) throws Exception {Object handler = this.handlerMap.get(urlPath);if (handler != null) {if (handler instanceof String) {String handlerName = (String)handler;handler = this.getApplicationContext().getBean(handlerName);}this.validateHandler(handler, request);return this.buildPathExposingHandler(handler, urlPath, urlPath, (Map)null);} else {List<String> matchingPatterns = new ArrayList();Iterator var5 = this.handlerMap.keySet().iterator();while(var5.hasNext()) {String registeredPattern = (String)var5.next();if (this.getPathMatcher().match(registeredPattern, urlPath)) {matchingPatterns.add(registeredPattern);}}String bestPatternMatch = null;Comparator<String> patternComparator = this.getPathMatcher().getPatternComparator(urlPath);if (!matchingPatterns.isEmpty()) {Collections.sort(matchingPatterns, patternComparator);if (this.logger.isDebugEnabled()) {this.logger.debug("Matching patterns for request [" + urlPath + "] are " + matchingPatterns);}bestPatternMatch = (String)matchingPatterns.get(0);}if (bestPatternMatch != null) {handler = this.handlerMap.get(bestPatternMatch);String pathWithinMapping;if (handler instanceof String) {pathWithinMapping = (String)handler;handler = this.getApplicationContext().getBean(pathWithinMapping);}this.validateHandler(handler, request);pathWithinMapping = this.getPathMatcher().extractPathWithinPattern(bestPatternMatch, urlPath);Map<String, String> uriTemplateVariables = new LinkedHashMap();Iterator var9 = matchingPatterns.iterator();while(var9.hasNext()) {String matchingPattern = (String)var9.next();if (patternComparator.compare(bestPatternMatch, matchingPattern) == 0) {Map<String, String> vars = this.getPathMatcher().extractUriTemplateVariables(matchingPattern, urlPath);Map<String, String> decodedVars = this.getUrlPathHelper().decodePathVariables(request, vars);uriTemplateVariables.putAll(decodedVars);}}if (this.logger.isDebugEnabled()) {this.logger.debug("URI Template variables for request [" + urlPath + "] are " + uriTemplateVariables);}return this.buildPathExposingHandler(handler, bestPatternMatch, pathWithinMapping, uriTemplateVariables);} else {return null;}}}
- 属羊和属虎的婚姻匹配
- 人事匹配的定义
- 字符串的模式匹配
- 微信怎么关闭通讯录匹配
- 摩羯座匹配的星座配对是什么
- 第五人格匹配怎么开语音
- 猴女+男羊匹配吗
- 怎么使用vlookup函数匹配
- 速腾钥匙换电池后如何匹配
- 科目三速度与档位匹配的标准
