springboot静态资源配置

将jquery.js和index.html放在src/main/resource源目录下

springboot静态资源配置_第1张图片

输入地址 ,发现找不到资源

http://localhost:8088/jquery.js

springboot静态资源配置_第2张图片

1.默认地址

查看源码WebMvcAutoConfiguration.java中的WebMvcAutoConfigurationAdapter静态内部类WebMvcAutoConfigurationAdapter中的addResourceHandlers方法

            @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));
			}
		}

跟踪this.resourceProperties.getStaticLocations()可以看到默认查找的地址

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
			"classpath:/META-INF/resources/", "classpath:/resources/",
			"classpath:/static/", "classpath:/public/" };

将资源放在这四个位置,springboot可以找到.

优先级顺序为:META-INF/resources > resources > static > public 

2.继承WebMvcConfigurerAdapter或者实现WebMvcConfigurer

package com.spring.boot.config.web;

import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@EnableWebMvc
@Configuration
public class mvcConfig extends WebMvcConfigurerAdapter{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/** 访问都映射到classpath:/ 目录下
        //也可配置/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/**").addResourceLocations("classpath:/");
    }
}

如果非必要的情况不建议这样配置,因为这样配置springboot的web自动配置将不生效,其它项需要自己配置

3.application.properties配置

spring.resources.static-locations=classpath:/

可以配合spring.mvc.static-path-pattern使用

spring.resources.static-locations=classpath:/
spring.mvc.static-path-pattern=/static/**

访问路径就变为

http://localhost:8080/static/jquery.js

4.maven编译打包

在pom.xml中配置


        
            
                src/main/resources
                
                META-INF/resources
                
                
                    index.html
                    jquery.js
                
            
            
                src/main/resources
                
                
                    index.html
                    jquery.js
                
                
                    **
                
            
        
    

查看编译后的格式

springboot静态资源配置_第3张图片

你可能感兴趣的:(springboot)