博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DispatcherServlet 匹配请求路径时的疑惑
阅读量:6138 次
发布时间:2019-06-21

本文共 2942 字,大约阅读时间需要 9 分钟。

存在Servlet A,其url-pattern为 /A/*,则形如/A,/A/bcd,/A/b/c/d等式样的请求全部能够被Servlet A匹配上。

但是SpringMVC的DispatcherServlet的url-pattern /A/*只能匹配上/A式样的请求,/A/abc,/A/b/c等式样的均匹配不上。
debug发现,org.springframework.web.util.UrlPathHelper.getPathWithinServletMapping()方法会将请求路径中与url-pattern配置一致的路径(servletPath)截去,然后用剩下的部分去匹配,不知这么做的用意是什么,还请各位不吝赐教

/**     * Return the path within the servlet mapping for the given request,     * i.e. the part of the request's URL beyond the part that called the servlet,     * or "" if the whole URL has been used to identify the servlet.     * 

Detects include request URL if called within a RequestDispatcher include. *

E.g.: servlet mapping = "/*"; request URI = "/test/a" -> "/test/a". *

E.g.: servlet mapping = "/"; request URI = "/test/a" -> "/test/a". *

E.g.: servlet mapping = "/test/*"; request URI = "/test/a" -> "/a". *

E.g.: servlet mapping = "/test"; request URI = "/test" -> "". *

E.g.: servlet mapping = "/*.test"; request URI = "/a.test" -> "". * @param request current HTTP request * @return the path within the servlet mapping, or "" */ public String getPathWithinServletMapping(HttpServletRequest request) { String pathWithinApp = getPathWithinApplication(request); String servletPath = getServletPath(request); String sanitizedPathWithinApp = getSanitizedPath(pathWithinApp); String path; // 不能理解此处的用意何在 // If the app container sanitized the servletPath, check against the sanitized version if (servletPath.contains(sanitizedPathWithinApp)) { path = getRemainingPath(sanitizedPathWithinApp, servletPath, false); } else { path = getRemainingPath(pathWithinApp, servletPath, false); } if (path != null) { // Normal case: URI contains servlet path. return path; } else { // Special case: URI is different from servlet path. String pathInfo = request.getPathInfo(); if (pathInfo != null) { // Use path info if available. Indicates index page within a servlet mapping? // e.g. with index page: URI="/", servletPath="/index.html" return pathInfo; } if (!this.urlDecode) { // No path info... (not mapped by prefix, nor by extension, nor "/*") // For the default servlet mapping (i.e. "/"), urlDecode=false can // cause issues since getServletPath() returns a decoded path. // If decoding pathWithinApp yields a match just use pathWithinApp. path = getRemainingPath(decodeInternal(request, pathWithinApp), servletPath, false); if (path != null) { return pathWithinApp; } } // Otherwise, use the full servlet path. return servletPath; } }

  

转载于:https://www.cnblogs.com/dousnl/p/9802559.html

你可能感兴趣的文章
学习笔记之Data Visualization
查看>>
Leetcode 3. Longest Substring Without Repeating Characters
查看>>
【FJOI2015】金币换位问题
查看>>
数学之美系列二十 -- 自然语言处理的教父 马库斯
查看>>
Android实现自定义位置无标题Dialog
查看>>
面试总结
查看>>
Chrome浏览器播放HTML5音频没声音的解决方案
查看>>
easyui datagrid 行编辑功能
查看>>
HDU 2818 (矢量并查集)
查看>>
实验二 Java面向对象程序设计
查看>>
------__________________________9余数定理-__________ 1163______________
查看>>
webapp返回上一页 处理
查看>>
新安装的WAMP中phpmyadmin的密码问题
查看>>
20172303 2017-2018-2 《程序设计与数据结构》第5周学习总结
查看>>
eclipse中将一个项目作为library导入另一个项目中
查看>>
Go语言学习(五)----- 数组
查看>>
Android源码学习之观察者模式应用
查看>>
416. Partition Equal Subset Sum
查看>>
Django之FBV与CBV
查看>>
Vue之项目搭建
查看>>