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:
org.webjars
jquery
3.3.1-1
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:
请求:http://localhost:8080/asserts/js/Chart.min.js即可,注意路径中没有static
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 resolveFaviconLocations() {
String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
List 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在改变静态资源的时候也需要重启项目