在實際項目開發中,除了程序代碼外,還需要一些靜態資源,比如公司logo,背景圖,css樣式文件,js文件等等,這里介紹一下單體應用中Spring Boot對靜態資源的一些映射規則。(此處的單體應用指非前后端分離、非微服務、非SOA架構的簡易版項目,具體區別看下圖所示)
Spring Boot對靜態資源的映射規則
在Spring Boot中,SpringMVC的相關配置都默認在WebMvcAutoConfiguration類中,具體源碼請在IDE中自行搜索查看。
1、 所有/webjars/**(/**表示訪問此路徑下的任何資源,都會去classpath:/META-INF/resources/webjars/下尋找資源(webjars就是以jar包方式引入資源到項目中), 相關源碼如下:
// WebMvcAutoConfiguration.java
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) - > {
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
registration.addResourceLocations(resource);
}
});
}
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, String... locations) {
addResourceHandler(registry, pattern, (registration) - > registration.addResourceLocations(locations));
}
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern,
Consumer< ResourceHandlerRegistration > customizer) {
if (registry.hasMappingForPattern(pattern)) {
return;
}
ResourceHandlerRegistration registration = registry.addResourceHandler(pattern);
customizer.accept(registration);
registration.setCachePeriod(getSeconds(this.resourceProperties.getCache().getPeriod()));
registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());
registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified());
customizeResourceHandlerRegistration(registration);
}
結構如圖所示(以jquery為例):
jquery的maven依賴如下:
< dependency >
< groupId >org.webjars.npm< /groupId >
< artifactId >jquery< /artifactId >
< version >3.6.0< /version >
< /dependency >
訪問示例地址如下:
localhost:8080/webjars/jquery/3.6.0/dist/jquery.js
訪問結果如下圖所示:
2、 /**,訪問當前項目下的任何靜態資源,相關源碼如下:
// WebMvcAutoConfiguration.java
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) - > {
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
registration.addResourceLocations(resource);
}
});
// WebMvcProperties.java
public String getStaticPathPattern() {
return this.staticPathPattern;
}
private String staticPathPattern = "/**";
// WebProperties.java
public String[] getStaticLocations() {
return this.staticLocations;
}
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
/**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
由源碼可知,靜態資源的訪問路徑有如下幾個:
1、classpath:/META-INF/resources/ ;
2、classpath:/resources/;
3、classpath:/static/;
4、classpath:/public/;
5、/ (當前項目的根路徑)。
如下圖所示:
**3、 **歡迎頁: 靜態資源文件夾下的index.html頁面,相關源碼如下:
// WebMvcAutoConfiguration.java
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
return welcomePageHandlerMapping;
}
private Resource getWelcomePage() {
for (String location : this.resourceProperties.getStaticLocations()) {
Resource indexHtml = getIndexHtml(location);
if (indexHtml != null) {
return indexHtml;
}
}
ServletContext servletContext = getServletContext();
if (servletContext != null) {
return getIndexHtml(new ServletContextResource(servletContext, SERVLET_LOCATION));
}
return null;
}
private Resource getIndexHtml(String location) {
return getIndexHtml(this.resourceLoader.getResource(location));
}
private Resource getIndexHtml(Resource location) {
try {
Resource resource = location.createRelative("index.html");
if (resource.exists() && (resource.getURL() != null)) {
return resource;
}
}
catch (Exception ex) {
}
return null;
}
// WebMvcProperties.java
public String getStaticPathPattern() {
return this.staticPathPattern;
}
private String staticPathPattern = "/**";
根據源碼可知,歡迎頁是被/**映射,也解釋了首頁名稱為index.html的原因。
4、 自定義靜態資源文件夾,在配置文件application.properties中添加如下配置,就會覆蓋掉項目的默認配置,示例代碼如下:
spring.web.resources.static-locations=classpath:/brevity/,classpath:/github/
以上就是Spring Boot單體應用中關于靜態資源映射的說明。
-
CSS
+關注
關注
0文章
109瀏覽量
14371 -
MVC
+關注
關注
0文章
73瀏覽量
13852 -
SOA技術
+關注
關注
0文章
4瀏覽量
5578
發布評論請先 登錄
相關推薦
評論