SpringBoot对静态资源配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qwqw3333333/article/details/81774617

SpringBoot中SpringMVC的相关配置都在WebMvcAuotConfiguration这个类中

1 addResourceHandlers:添加资源映射

/**
  1 所有/webjars/**,都去classpath:/META-INF/resources/webjars/ 找资源;webjars:以jar包的 
    方式引入静态资源;网址http://www.webjars.org。如:
    <dependency>
	  <groupId>org.webjars</groupId>
	  <artifactId>jquery</artifactId>
	  <version>3.3.1-1</version>
    </dependency>
    访问:localhost:8080/webjars/jquery/3.3.1-1/jquery.js
  2 "/**"访问当前项目的任何资源,都去(静态资源的文件夹)找映射,默认的静态资源文件夹:
    "classpath:/META‐INF/resources/",
    "classpath:/resources/",
    "classpath:/static/",
    "classpath:/public/"
    "/":当前项目的根路径
*/
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(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
		}

	}
}

//可以自定义静态资源文件路径
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,\
  classpath:/static/,classpath:/public/,D:/files

2 配置欢迎页

//遍历静态资源文件夹下的所有index.html页面;然后被"/**"映射;
@Bean
public WebMvcAutoConfiguration.WelcomePageHandlerMapping welcomePageHandlerMapping(ResourceProperties resourceProperties) {
  return new WebMvcAutoConfiguration.WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),this.mvcProperties.getStaticPathPattern());
}

private String[] getStaticWelcomePageLocations() {
	String[] result = new String[this.staticLocations.length];

	for(int i = 0; i < result.length; ++i) {
		String location = this.staticLocations[i];
		if (!location.endsWith("/")) {
			location = location + "/";
		}

		result[i] = location + "index.html";
	}

	return result;
}

3 设置图标

//所有的**/favicon.ico 路径下图标,都是在静态资源文件下找资源
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(-2147483647);
		mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
		return mapping;
	}

	@Bean
	public ResourceHttpRequestHandler faviconRequestHandler() {
		ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
		requestHandler.setLocations(this.resourceProperties.getFaviconLocations());
		return requestHandler;
	}
}

4 可以设置和静态资源有关的参数,缓存时间等

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

猜你喜欢

转载自blog.csdn.net/qwqw3333333/article/details/81774617