在實際項目開發(fā)中,除了程序代碼外,還需要一些靜態(tài)資源,比如公司logo,背景圖,css樣式文件,js文件等等,這里介紹一下單體應(yīng)用中Spring Boot對靜態(tài)資源的一些映射規(guī)則。(此處的單體應(yīng)用指非前后端分離、非微服務(wù)、非SOA架構(gòu)的簡易版項目,具體區(qū)別看下圖所示)
Spring Boot對靜態(tài)資源的映射規(guī)則
在Spring Boot中,SpringMVC的相關(guān)配置都默認(rèn)在WebMvcAutoConfiguration類中,具體源碼請在IDE中自行搜索查看。
1、 所有/webjars/**(/**表示訪問此路徑下的任何資源,都會去classpath:/META-INF/resources/webjars/下尋找資源(webjars就是以jar包方式引入資源到項目中), 相關(guān)源碼如下:
// 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);
}
結(jié)構(gòu)如圖所示(以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
訪問結(jié)果如下圖所示:
2、 /**,訪問當(dāng)前項目下的任何靜態(tài)資源,相關(guān)源碼如下:
// 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;
由源碼可知,靜態(tài)資源的訪問路徑有如下幾個:
1、classpath:/META-INF/resources/ ;
2、classpath:/resources/;
3、classpath:/static/;
4、classpath:/public/;
5、/ (當(dāng)前項目的根路徑)。
如下圖所示:
**3、 **歡迎頁: 靜態(tài)資源文件夾下的index.html頁面,相關(guān)源碼如下:
// 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 = "/**";
根據(jù)源碼可知,歡迎頁是被/**映射,也解釋了首頁名稱為index.html的原因。
4、 自定義靜態(tài)資源文件夾,在配置文件application.properties中添加如下配置,就會覆蓋掉項目的默認(rèn)配置,示例代碼如下:
spring.web.resources.static-locations=classpath:/brevity/,classpath:/github/
以上就是Spring Boot單體應(yīng)用中關(guān)于靜態(tài)資源映射的說明。
-
CSS
+關(guān)注
關(guān)注
0文章
109瀏覽量
14340 -
MVC
+關(guān)注
關(guān)注
0文章
73瀏覽量
13831 -
SOA技術(shù)
+關(guān)注
關(guān)注
0文章
4瀏覽量
5574
發(fā)布評論請先 登錄
相關(guān)推薦
評論