springboot学习之静态资源的加载

SpringBoot对静态资源的映射规则

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements 

在application.properties中配置 spring.resources //可以设置和静态资源有关的参数,缓存时间等

springboot对于静态资源的加载主要是在 启动时的 WebMvcAutoConfiguration

这个启动自动配置类中下面可以看下其中部分代码

WebMvcAuotConfiguration:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META‐INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
//静态资源文件夹映射
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod));
}
}
//配置欢迎页映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(
ResourceProperties resourceProperties) {
return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
}
//配置喜欢的图标
@Configuration
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)

public static class FaviconConfiguration {
private final ResourceProperties resourceProperties;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
//所有 **/favicon.ico
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new
ResourceHttpRequestHandler();
requestHandler
.setLocations(this.resourceProperties.getFaviconLocations());
return requestHandler;
}
}

1、对于引入的前端框架,所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;

       webjars:以jar包的方式引入静态资源;

         http://www.webjars.org/

           

在pom.xml中引入依赖

<!‐‐引入jquery‐webjar‐‐>在访问的时候只需要写webjars下面资源的名称即可
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.0</version>
</dependency

2、如果需要访问页面   "/**" 访问当前项目的任何资源,都去(静态资源的文件夹)找映射

   路径为下面:

"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径

3、应用欢迎页面或者首页  欢迎页; 静态资源文件夹下的所有index.html页面;被"/**"映射;

        还记得Web应用启动后的默认欢迎页index.html吗?Spring Boot支持静态欢迎页和模板欢迎页,应用启动会到/static或/public或/resources或/META-INF/resources目录下寻找index.html,如果自定义了静态资源存储路径,那么就到自定义路径下寻找index.html。对这个index.html进行访问的时候,默认使用/index.html可以访问,如果自定义了静态资源访问路径,那么就需要使用自定义静态资源访问路径,例如/image/index.html。如果找不到index.html,将搜索index的模板文件,如果找到了,它将自动用作应用程序的欢迎页面

   

4、应用图标  所有的 **/favicon.ico 都是在静态资源文件下找;

    使用浏览器访问Web服务的时候,网页标签的左上角会显示应用图标,这个图标的名称是favicon.ico,如果没有修改默认静态资源存储路径,那么它可以存放在/static或/public或/resources或/META-INF/resources目录下,如果修改了,就需要存到自定义路径下。

猜你喜欢

转载自blog.csdn.net/qq_36697880/article/details/81538474
今日推荐