1024programmer Blog springmvc reports error, cannot find static resource_mvc:resources reports error_Bai Xiaosen’s blog

springmvc reports error, cannot find static resource_mvc:resources reports error_Bai Xiaosen’s blog

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
Insert picture description here

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
in  Insert the picture description here
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

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/springmvc-reports-error-cannot-find-static-resource_mvcresources-reports-error_bai-xiaosens-blog/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索