In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the implementation of the five mapping rules for static resources in SpringBoot, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
The SpringMVC configuration function in SpringBoot is in the WebMvcAutoConfiguration class, and xxxxAutoConfiguration helps us to automatically configure components in the container. The shortcut key for idea global search is shift twice. Check webMvcAutoConfiguration to view the webMvc automatic configuration class.
The principle of the WebMvcAutoConfiguration class will be mastered at least a little in the future, and here the article is just to look at its specific key code. Here, I only give an example of some of the key code. I have a headache if I look at it. It doesn't matter if I don't understand it. You can skip the source code stage, so why bother?
Public void addResourceHandlers (ResourceHandlerRegistry registry) {if (! this.resourceProperties.isAddMappings ()) {logger.debug ("Default resource handling disabled");} else {Duration cachePeriod = this.resourceProperties.getCache () .getPeriod (); CacheControl cacheControl = this.resourceProperties.getCache () .getCachecontrol () .toHttpCacheControl () If (! registry.hasMappingForPattern ("/ webjars/**")) {this.customizeResourceHandlerRegistration (registry.addResourceHandler (new String [] {"/ webjars/**"}) .addResourceLocations (new String [] {"classpath:/META-INF/resources/webjars/"}) .setCachePeriod (this.getSeconds (cachePeriod)) .setCacheControl (cacheControl);} String staticPathPattern = this.mvcProperties.getStaticPathPattern () If (! registry.hasMappingForPattern (staticPathPattern)) {this.customizeResourceHandlerRegistration (registry.addResourceHandler (new String [] {staticPathPattern}) .addResourceLocations (WebMvcAutoConfiguration.getResourceLocations (this.resourceProperties.getStaticLocations () .setCachePeriod (this.getSeconds (cachePeriod)) .setCacheControl (cacheControl));}}
All right, the following parts are the core of this article! Come on, Ollie. Here!
1. Webjars: introduce static resources in the form of jar packages
We used to have a webapp directory in the web project to store static resources. Although there is no file path in springboot, springboot still has its own rules. From the above code, you can see that all / webjars/** will look for resources in classpath:/META-INF/resources/webjars/. So what is webjars? Webjars introduces static resources in the form of jar packages.
Webjars's official website: https://www.webjars.org/ [Baidu webjars is also available]. After entering, do the following:
Just copy the above dependency into the dependency of the maven project, as follows
Org.webjars jquery 3.4.1
After that, all / webjars/** will look for resources in classpath:/META-INF/resources/webjars/, and this path is in the following directory
After removing the check box, the small box on the right is the path in our corresponding classpath:/META-INF/resources/webjars/.
After the above steps are completed, for example, we start to access the jquery.js resource in jQuery to run the project directly, and then enter http://localhost:8080/webjars/jquery/3.4.1/jquery.js in the address bar to access the jquery.js file under the corresponding path. The results of the page are as follows:
Indicates that the access to static resources is successful. Yes, the above method is the way to import jar package, if we want to use some of our own JS, CSS, jQuery files, can we? The answer is yes, so there is our second way to appear.
2. / * * access any resources of the current project
If it is your own static resource files, such as some of your own JS, CSS and jQuery files, SpringBoot is read from the following paths by default
"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" / ": the root path of the current project
If http://localhost:8080/jquery.js does not have any code processing, it is equivalent to going directly to the static resource folder to access jquery.js, as follows
3. Home page index.html, mapped by "/ *"
If you add a file named index.html under the default static resource access path of SpringBoot, accessing localhost:8080 will automatically jump to this index.html. In other words, the default first page of SpringBoot is index.html, which is mapped by "/ *". As follows:
4. Custom icon / favicon.ico
In SpringBoot, the icon in ico format can be placed under the default static resource file path and named after favicon.ico, and the application icon will automatically become the specified icon. All / favicon.ico will be found under the static resource file, as follows:
5. Manually configure the static resource access path in application.properties
Edit the following in the application.properties configuration file:
# Custom static resource access paths, which can be specified with spring.resources.static-locations=classpath:/myabc/,classpath:/myhhh separated by commas
As described in the above comments, custom static resource access paths can be specified, separated by commas, and it is important to note that after customizing static resources, SpringBoot's default static resource paths will no longer work!
After reading the above, do you have any further understanding of the implementation of the five mapping rules for static resources in SpringBoot? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.