In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
SpringBoot how to start the path of class static resources, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
SpringBoot startup class static resource path SpringBoot core configuration class
SpringBoot Core JAR package-"spring-boot-autoconfigure-2.2.6.RELEASE.jar"
And under it is-- "org.springframework.boot.autoconfigure.web."
There is a class-- "ResourceProperties".
Class to define default static resource access as follows:
Private static final String [] CLASSPATH_RESOURCE_LOCATIONS = {"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"}; these are the following four static resource paths
/ META-INF/resources/
/ resources/
/ static/
/ public/
Static resources are all in classpath, so the question is, where is the classpath in IDEA? Let's take a look.
Click to enter the structural design of the project-"first, open File- > Project Structure..., in the open project window to get the project structure shown in the following figure:
Source Folders represents the code source files directory, the generated class files will be output to the target- > classess folder, but the source files inside will not be copied to the target- > classes folder, Test Source Folders represents the test code source files directory, the generated class files will also be exported to the target- > classess folder, and the source files inside will not be copied to the target- > classes folder.
Recource Folders represents the resource file directory, and the files in these directories are copied directly to the target- > classess folder during the code compilation run. Target- > classes is classpath, and any resources we need to get in the classpath prefix must be found in the target- > classes folder, otherwise an error message for java.io.FileNotFoundException will appear.
If you want to add some of your own directories to directory categories such as Source Folders and Resource Folders to achieve correct file output after compilation, you can click on the folder you want to set, and then select the correct directory type at the top of the file structure window, Make as: later.
So the folder access of our project static resources is based on the Resource Folder here, for example:
Classpath:/static/ means: src/main/resources/static.
So the default index.xml for SpringBoot can be placed in a folder with the path src/main/resources/static, so that the index.html is automatically loaded as soon as the project starts.
We set src to Resource Folders, and then we create the / META-INF/resources/ path under src:
We start the project (there is no class created at this time, the startup class starts to load the default index)
Successfully display the content of index.html, no problem. There is no problem with the other paths
There is a problem here, that is, the order of people in these paths. We closed the establishment of such four paths, setting different contents:
After startup, the output is still HelloWorld. Now let's delete the index.html of / META-INF/resources/ and take a look:
Output successfully. The rest will not be tried, and it is clear that the default loading order is in the order in the source code.
They are:
/ META-INF/resources/
/ resources/
/ static/
/ public/
Private String [] appendSlashIfNecessary (String [] staticLocations) {String [] normalized = new String [staticLocations.length]; for (int I = 0; I
< staticLocations.length; i++) { String location = staticLocations[i]; normalized[i] = location.endsWith("/") ? location : location + "/"; } return normalized;}//循环遍历存放这几个路径,按照顺序加载。静态文件存放位置设置默认配置 SpringBoot把类路径下的/static,/public,/resources和META-INF/resources文件下的静态文件映射为/,可以通过http://localhost:8080/访问 SpringBoot默认了静态文件的位置src/main/resources下的static目录,如下: static目录需要自己创建。HTML也可以放在里面 src/main/resources |_static |_js |_img |_demo.png |_css 在html代码中使用一下代码即可访问图片Custom location
Add a directory location
Src/main/resources | _ myImg | _ demo.png | _ static | _ js | _ img | _ css
Add a @ Configuration annotated configuration class
Import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter / * * Custom image path * @ author pzr * * / @ Configurationpublic class MyImageAddr extends WebMvcConfigurerAdapter {@ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {/ / addResourceHandler is the access path, which can be modified to other strings / / addResourceLocations where the actual path is registry.addResourceHandler ("/ myImg/**"). AddResourceLocations ("classpath:/myImg/"); super.addResourceHandlers (registry);}}
You can access the picture by using the code in the html code
This is the answer to the question about how SpringBoot starts the path of class static resources. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.