1. 程式人生 > 其它 >Spring 工具類-AntPathMatcher

Spring 工具類-AntPathMatcher

技術標籤:springAntPathMatcherspring

文章目錄

原始碼

AntPathMatcher是org.springframework.util工具包下的方法。

/**
* A convenient, alternative constructor to use with a custom path separator.
 * @param pathSeparator the path separator to use, must not be {@code null}.
 * @since 4.1
 */
public AntPathMatcher
(String pathSeparator) { Assert.notNull(pathSeparator, "'pathSeparator' is required"); this.pathSeparator = pathSeparator; this.pathSeparatorPatternCache = new PathSeparatorPatternCache(pathSeparator); }

使用

public boolean hasUrl(String url) {
    if (url == null || "".equals
(url)) { return false; } AntPathMatcher antPathMatcher = new AntPathMatcher(); // 可根據需求做動態匹配 String pattern = "/app/*.html" if (antPathMatcher.match(pattern, url)) { // 根據入參url和pattern匹配上返回ture,否則false. return true; } return false; }

匹配例子

以下是模糊匹配規則,也就是在響應的路徑上新增* 或則 ** 對路徑進行替代即可。

URL路徑說明
/app/*.x匹配(Matches)所有在app路徑下的.x檔案
/app/p?ttern匹配(Matches) /app/pattern 和 /app/pXttern,但是不包括/app/pttern
/**/example匹配(Matches) /app/example, /app/foo/example, 和 /example
/app/**/dir/file.匹配(Matches) /app/dir/file.jsp, /app/foo/dir/file.html,/app/foo/bar/dir/file.pdf, 和 /app/dir/file.java
/**/*.jsp匹配(Matches)任何的.jsp 檔案