Filter mapping url-pattern that excludes subdirectories

前端 未结 5 1178
遥遥无期
遥遥无期 2020-12-30 13:48

Is there any way to make a filtermapping not include subdirectories?

For example.

I have .xhtml files in my context root, and I also have a subfolder named \

5条回答
  •  遥遥无期
    2020-12-30 14:10

    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.

    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.
    }
    

    The HttpServletRequest#getServletPath() basically returns the part of the request URI from the context path on.

    You can if necessary configure the value /test as an of the filter so that you can control the value from inside the web.xml instead of in the Filter's code.

提交回复
热议问题