If your DispatcherServlet intercepts a URL with a suffix such as "*. do", you will not be able to access static resources.
If your DispatcherServlet intercepts "/" and intercepts all requests in order to implement the REST style, access to static files such as *. js and *. jpg will also be blocked.
Solve this problem.
Purpose: To access a static file normally. If the static file cannot be found, the Error 404 is returned. Solution 1: Activate Tomcat's defaservlet servlet to process static files
default
*.jpg
default
*.js
default
*.css
Multiple configurations are required. One configuration for each file must be written before the DispatcherServlet, so that the defaultServlet can intercept the request first, so that the request will not enter Spring.
Tomcat, Jetty, JBoss, and GlassFish default Servlet name -- "default"
Name of the default Servlet that comes with Google App Engine -- "_ ah_default"
Default Servlet name provided by Resin --
"Resin-file"
Default Servlet name provided by WebLogic
-- "FileServlet"
WebSphere default Servlet name -- "SimpleFileServlet" solution 2: mvc: resources is provided in spring3.0.4 and later versions. Usage:
/Images/** ing to ResourceHttpRequestHandler for processing. location specifies the location of static resources. it can be under the web application root directory and in the jar package, so that static resources can be compressed into the jar package. Cache-period allows static resources to be used for web cache
The following error may be caused by no configuration. .
Error WARNING: No mapping found for HTTP request with URI
[/Mvc/user/findUser/lisi/770] in DispatcherServlet with name 'springmvc'
Use And register the URI of simpleing to the urlMap of SimpleUrlHandlerMapping,
Key is the URI pattern value of mapping, and value is ResourceHttpRequestHandler,
In this way, the access to static resources is converted from HandlerMapping to ResourceHttpRequestHandler for processing and returning. Therefore, the classpath directory and the access to static Resources in the jar package are supported.
In addition, do not set defaultHandler for SimpleUrlHandlerMapping. Because the defaultHandler for static uri is ResourceHttpRequestHandler,
Otherwise, the static resources request cannot be processed. solution 3: Use
The "/**" url is registered to the urlMap of SimpleUrlHandlerMapping, and the access to static resources is transferred from HandlerMapping to org. springframework. web. servlet. resource. defaultServletHttpRequestHandler process and return.
DefaultServletHttpRequestHandler is the default Servlet used by each Servlet container. Note: The execution sequence of multiple HandlerMapping problems: the order attribute value of DefaultAnnotationHandlerMapping is: 0.
The order attribute value of automatically registered SimpleUrlHandlerMapping is: 2147483646 The order attribute value of automatically registered SimpleUrlHandlerMapping is: 2147483647spring will first execute the order value smaller. When copying an a.jpg image file, you must first use DefaultAnnotationHandlerMapping to find the handler. It must be because we didn't call the.jpg Action. Then search by order value in ascending order. Because the last SimpleUrlHandlerMapping matches "/**", the image will be matched and the image will be responded.
To access an image, perform layer-by-layer matching. What is the performance?Finally, solution 2 and solution 3 use the Interceptor to access static resources. If you implement permission check in interception, you must filter these requests for static files.
How to intercept URLs such as *. do in your DispatcherServlet does not save the above problem. There is a suffix for convenience.