1.webjars
webjars:以jar包形式引入的静态资源;https://www.webjars.org/;我们可以在这里找到很多我们需要的前端框架。
而所有的/webjars/**,都会去classpath:/META-INF/resources/webjars/ 下面寻找资源,所谓类路径,我们在项目中引入jquery的webjars实例如下:
SpringBoot的web项目默认可以通过/webjars/**命令访问通过jar包引入的资源。http://localhost:8080/webjars/jquery/3.4.1/jquery.js可以访问。
2.项目内的静态资源映射规则
2.1 一般的静态资源映射
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties {
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/"},这些就是默认的静态资源的存放文件夹。classpath的路径是:
也就是说在java和resources下创建resources&static&public&META-INF/resouces都可以被SpringBoot扫描到。
而它的映射方式是
public WebMvcProperties() {
this.localeResolver = WebMvcProperties.LocaleResolver.ACCEPT_HEADER;
this.dispatchTraceRequest = false;
this.dispatchOptionsRequest = true;
this.ignoreDefaultModelOnRedirect = true;
this.throwExceptionIfNoHandlerFound = false;
this.logResolvedException = false;
this.staticPathPattern = "/**"; //映射替代
this.async = new WebMvcProperties.Async();
this.servlet = new WebMvcProperties.Servlet();
this.view = new WebMvcProperties.View();
this.contentnegotiation = new WebMvcProperties.Contentnegotiation();
this.pathmatch = new WebMvcProperties.Pathmatch();
}
根据this.staticPathPattern = "/**"; 我们知道以上默认的四种路径映射被"/**"所替代。所以我们在浏览器请求中只需要直接在项目路径后输入对应的"/resourceName.xxx",SpringBoot就会自动在以上几个静态文件夹中去扫描相应的静态文件。SpringBoot还有首页的自动配置,如果什么都不输入,会自动去寻找index.html的页面进行反馈。
2.2 自动配置网页小图标
@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.getResourceLocations(this.resourceProperties.getStaticLocations());
List<Resource> locations = new ArrayList(staticLocations.length + 1);
Stream var10000 = Arrays.stream(staticLocations);
ResourceLoader var10001 = this.resourceLoader;
var10001.getClass();
var10000.map(var10001::getResource).forEach(locations::add);
locations.add(new ClassPathResource("/"));
return Collections.unmodifiableList(locations);
}
}
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));所有的"**/favicon.ico",继续看下面的方法:
可以看到它还是会在刚刚说的四个静态资源文件夹下面去找。然后将这个叫做favicon.ico的ico图标自动加载为当前项目的web图标。
2.3 自定义静态资源访问路径
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties {
在application.properties中修改spring.resources属性就可以修改默认的静态资源访问路径。
#修改默认的静态资源访问文件路径
spring.resources.static-locations=classpath:/hello/,classpath:/njusoftware/