Mapping a global front controller servlet, such as the Spring MVC dispatcher, on /* URL pattern provides centralized control for handling incoming requests. However, this mapping can interfere with access to static resources, like CSS files, JavaScript scripts, and images.
To resolve this issue, a multifaceted approach is required:
In the filter's doFilter() method, implement the following logic:
HttpServletRequest req = (HttpServletRequest) request; String path = req.getRequestURI().substring(req.getContextPath().length()); if (path.startsWith("/static")) { chain.doFilter(request, response); // Goes to default servlet. } else { request.getRequestDispatcher("/pages" + path).forward(request, response); }
This filter allows static resources to be served without any changes to the browser address bar. Additionally, you can customize the "/static" and "/pages" paths by using initialization parameters in the filter.
The above is the detailed content of How to Serve Static Resources When Using a Global Front Controller Servlet Mapped to /*?. For more information, please follow other related articles on the PHP Chinese website!