过滤映射url-pattern,排除子目录 [英] Filter mapping url-pattern that excludes subdirectories
问题描述
有没有办法让filtermapping不包含子目录?
Is there any way to make a filtermapping not include subdirectories?
例如。
我有我的上下文根目录中的.xhtml文件,我还有一个名为test的子文件夹,其文件具有相同的扩展名。有没有可能将过滤器映射到上下文根目录中的文件而不是test目录中的文件?
I have .xhtml files in my context root, and I also have a subfolder named "test" with files with the same extension. Is there any may to map a filter to the files in context root and not to the files in the "test" directory?
推荐答案
url-pattern
在匹配方面确实存在限制。它只允许精确,前缀或后缀matchnig。不在中间/整体/正则表达式匹配。所以例如 / *。xhtml
您打算做什么不会起作用。
The url-pattern
is indeed restrictive in matching. It only allows exact, prefix or suffix matchnig. Not midst/overall/regex matching. So e.g. /*.xhtml
what you intend to do ain't going to work.
如果你只想在 / test
文件夹中排除XHTML,那么你最好的是过滤
收听 url-pattern
*。xhtml
基本上做什么 doFilter()
方法中的以下作业:
If you want to exclude XHTML in the /test
folder only, then your best is really a Filter
listening on an url-pattern
of *.xhtml
which does basically the following job in doFilter()
method:
// First cast ServletRequest to HttpServletRequest.
HttpServletRequest hsr = (HttpServletRequest) request;
// Check if requested resource is not in /test folder.
if (!hsr.getServletPath().startsWith("/test/")) {
// Not in /test folder. Do your thing here.
}
HttpServletRequest#getServletPath()
基本上从上下文路径返回请求URI的一部分。
The HttpServletRequest#getServletPath()
basically returns the part of the request URI from the context path on.
您可以根据需要配置值 / test
作为过滤器的< init-param>
,以便您可以从 web.xml
而不是Filter的代码。
You can if necessary configure the value /test
as an <init-param>
of the filter so that you can control the value from inside the web.xml
instead of in the Filter's code.
这篇关于过滤映射url-pattern,排除子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!