Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of introducing static Resources in SpringBoot+thymeleaf

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "the method of introducing SpringBoot+thymeleaf static resources", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "the method of introducing SpringBoot+thymeleaf static resources" can help you solve your doubts.

I. Mapping rules of static resources

1. Which directories are mapped?

Classpath:/META-INF/resources/

Classpath:/resources/

Classpath:/static/

Classpath:/public/

/: root path of the current project

We put static resource files (such as jq.js, etc.) in the above five directories, which can be accessed directly (similar to the webapp of the previous web project, which cannot be accessed in other directories.

two。 Why these catalogs?

2.1 just look at the source code

WebMvcAutoConfirarution.java classes automatically configured by SpringBoot:

@ Override

Public void addResourceHandlers (ResourceHandlerRegistry registry) {

If (! this.resourceProperties.isAddMappings ()) {

Logger.debug ("Default resource handling disabled")

Return

}

Duration cachePeriod=this.resourceProperties.getCache () .getPeriod ()

CacheControl cacheControl=this.resourceProperties.getCache ()

.getCachecontrol () .toHttpCacheControl ()

If (! registry.hasMappingForPattern ("/ webjars/**")) {

CustomizeResourceHandlerRegistration (registry

.addResourceHandler ("/ webjars/**")

.addResourceLocations ("classpath:/META-INF/resources/webjars/")

.setCachePeriod (getSeconds (cachePeriod))

.setCacheControl (cacheControl))

}

String staticPathPattern=this.mvcProperties.getStaticPathPattern ()

If (! registry.hasMappingForPattern (staticPathPattern)) {

CustomizeResourceHandlerRegistration (

Registry.addResourceHandler (staticPathPattern)

.addResourceLocations (getResourceLocations (

This.resourceProperties.getStaticLocations ())

.setCachePeriod (getSeconds (cachePeriod))

.setCacheControl (cacheControl))

}

}

ResourceProperties

ConfigurationProperties (prefix= "spring.resources", ignoreUnknownFields=false)

Public class ResourceProperties {

/ / We can see the mapping path of static resources

Private static final String [] CLASSPATH_RESOURCE_LOCATIONS= {

"classpath:/META-INF/resources/", "classpath:/resources/"

Classpath:/static/ "," classpath:/public/ "}

...

}

In general:

The WebMvcAutoConfiguration class automatically registers the following directory for us as a static resource directory, that is, a directory that has direct access to resources.

Classpath:/META-INF/resources/

Classpath:/resources/

Classpath:/static/

Classpath:/public/

/: root path of the current project

Priority is from top to bottom.

So, if there is an index.html,public in the static and an index.html under it, then the index.html under the static will be loaded first because of the priority!

After reading this, the article "methods of introducing SpringBoot+thymeleaf static Resources" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, please 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report