In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to access static resources in Spring Boot. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
0. Two configuration spring.mvc.static-path-patternspring.resources.static-locations1. Two configuration items that need to go first in application
1.1 spring.mvc.static-path-pattern
This configuration item tells springboot how to find resources. The default configuration is / *. In other words, Spring Boot will process static resource requests only if the static resource satisfies what matching criteria
For example, the following configuration
Spring.mvc.static-path-pattern=/soul/*
If the previous address to visit the home page is: http://localhost:8888/index.html
Then after you configure this configuration, the above access becomes invalid, and now you need to visit the same page like this: http://localhost:8888/soul/index.html
Question 1: after configuring this configuration, http://localhost:8888 could access the home page directly, but now http://localhost:8888/soul cannot access the home page.
Postscript: after repeated use, it is found that as long as this configuration is configured, no matter what the configuration is, http://localhost:8888 will not be able to access the home page.
Question 2: after configuring the access path to / soul/, you can access static resources under the directories of other folders under resources, but now you can't. For example, in the screenshot below, test/test.html cannot be accessed, and / soul/ can be accessed through http://localhost:8888/test/test.html if it is not configured.
1.2 spring.resources.static-locations
This configuration item tells springboot where to find resources.
# default is spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
Priority is determined according to the context, that is, if both the / META-INF/resources/ directory and / resources have an index.html, the resources under / META-INF/resources/ will be accessed according to the default priority.
two。 An instance of a configuration
First, the project directory looks like this: static and tempaltes each have several pages, with a test page with the same name used to test the priority.
Since the established springboot project generally places static files such as js and css under static and pages under templates, only the resource directory is configured. The first configuration uses the default * / * *, as shown below.
Spring.resources.static-locations=classpath:/templates/,classpath:/static/
After configuring the startup project on port 8888, access http://localhost:8888/. According to the above theory, you should access the index.html under static. The result is as shown below:
Access: http://localhost:8888/test.html, visually access the test.html under templates, because the priority of templates in the configuration is higher than that of static. The result is shown in the figure.
The above is how to access static resources in Spring Boot. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.