1. 程式人生 > >SpringBoot——web開發之靜態資源對映

SpringBoot——web開發之靜態資源對映

1、通過/webjars/**請求靜態資源

SpringMVC的相關配置都在WebMvcAutoConfiguration類中,在該類中有一處新增資源對映:

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

	}
}

該段程式碼中明示對於所有/webjars/**的請求,如果沒有請求對映的處理方法(Controller)則當做靜態資源請求處理,都去"classpath:/META-INF/resources/webjars/"中找資源:

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

webjars:以jar包的方式引入靜態資源(參考:Web Libraries in Jars),以maven依賴的方式將靜態資源引入到專案中,eg:

<!-- jquery的webjar-->
<dependency>
	<groupId>org.webjars</groupId>
	<artifactId>jquery</artifactId>
	<version>3.3.1-1</version>
</dependency>

 jquery的webjars引入之後在依賴包中可以看到其目錄結構:

當我們發/webjars/**請求時會去專案中webjars對應目錄去找資源:比如我們要請求jquery.js,需發請求:localhost:8080/webjars/jquery/3.3.1-1/jquery.js,結果如下:

靜態資源相關的配置可以參照ResourceProperties,參考該類可以在主配置檔案中配置靜態資源的快取時間等:

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

2、自定義靜態資源(非webjars的靜態資源):

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

跟蹤程式碼可知staticPathPattern的值是/**,即對於所有的請求,如果沒有對映到處理方法,會當做靜態資源請求來處理,回去下面的這幾個路徑下去查詢靜態資源:

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

靜態資原始檔夾:

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

"classpath:/resources/":

"classpath:/static/":

"classpath:/public/":

"/":

就是說我們可以在這些資料夾下放靜態資源:

例如我們要訪問static下的Chart.min.js:

3、歡迎頁配置:

@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
	return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
}

其實就是靜態資原始檔夾下的index.html,在不指定具體資源的時候就會預設訪問該index.html,如果該index.html也不存在,則會返回錯誤SpringBoot的預設錯誤頁面:

private Resource getIndexHtml(String location) {
	return this.resourceLoader.getResource(location + "index.html");
}

例如:訪問localhost:8080即可訪問到index.html

4、配置喜歡的圖示,都是在靜態資原始檔下找(**/favicon.ico)

@Configuration
@ConditionalOnProperty(
	value = {"spring.mvc.favicon.enabled"},
	matchIfMissing = true
)
public static class FaviconConfiguration implements ResourceLoaderAware {
	private final ResourceProperties resourceProperties;
	private ResourceLoader resourceLoader;

	public FaviconConfiguration(ResourceProperties resourceProperties) {
		this.resourceProperties = resourceProperties;
	}

	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}

	@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.resolveFaviconLocations());
		return requestHandler;
	}

	private List<Resource> resolveFaviconLocations() {
		String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
		List<Resource> locations = new ArrayList(staticLocations.length + 1);
		Stream var10000 = Arrays.stream(staticLocations);
		ResourceLoader var10001 = this.resourceLoader;
		this.resourceLoader.getClass();
		var10000.map(var10001::getResource).forEach(locations::add);
		locations.add(new ClassPathResource("/"));
		return Collections.unmodifiableList(locations);
	}
}

例如:favicon.ico也可以放在其他靜態資原始檔夾的根路徑下

5、自定義靜態資原始檔夾:值是個陣列,配置之後預設的靜態資原始檔夾都會失效,無法訪問

spring.resources.static-locations=classpath:/hello/,classpath:/world/

注意:SpringBoot在改變靜態資源的時候也需要重啟專案