class=”markdown_views prism-atom-one-dark”>
1. Question
Static resources such as .png, .css, and .js cannot be found, resulting in no effect in many places on the webpage, as shown in the figure below
2. Solve
Through searching, it is found that the problem lies in springmvc, and the blocking is set in the web.xml file as follows
Although .png, .css, .js, etc. are intercepted, since springmvc has no corresponding processing for these requests, no static resources can be found, so we need to set something to help us deal with it These static resource requests
Method 1:
Use the default Servlet processing of the default web application server, configure it in the springmvc.xml configuration file
mvc:default-servlet-handler />
Method 2:
Use the Spring MVC framework to handle static resources by itself, and add some useful value-added functions
- allows static resources to be placed anywhere, such as under the WEB-INF directory, under the class path, etc., and you can even put static files such as JavaScript into the JAR package.
- Provide optimization for static resources according to the current famous Page Speed, YSlow and other browser optimization principles.
mvc:resources location="/,classpath:/META-INF/publicResources/" mapping= "/resources/**"/>
Description:
The location element represents all the files under the static package under the webapp directory;
The mapping element represents all request paths starting with /static, such as /static/a or /static/a/b;
The function of this configuration is: DispatcherServlet will not intercept all request paths starting with /static, and treat them as static resources
Handed over to the Servlet for processing.
Reference:
https://www.cnblogs.com/dflmg/p/6393416.html
https://www.cnblogs.com/linnuo/p/7699401.html