且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

过滤映射url-pattern,排除子目录

更新时间:2022-02-14 17:54:35

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.