1.SpringBoot对静态资源的映射规则
WebMvcAuotConfiguration:
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")){
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations( "classpath:/META‐INF/resources/webjars/")
相当于给/webjars/** 添加了映射 /META‐INF/resources/webjars/
1. 所以访问 /webjars/** 的路径, 都会去 classpath:/META-INF/resources/webjars/ 下找资源;
如:localhost:8080/webjars/jquery/3.3.1/jquery.js 相当于
localhost:8080/META‐INF/resources/webjars/jquery/3.3.1/jquery.js
2. “/**” 访问当前项目的任何资源,都去(静态资源的文件夹)找映射
public WebMvcProperties() {
this.localeResolver = WebMvcProperties.LocaleResolver.ACCEPT_HEADER;
this.staticPathPattern = "/**";
//静态资源文件夹映射路径
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"};
//将 CLASSPATH_RESOURCE_LOCATIONS与staticLocations绑定
public ResourceProperties() {
this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
//然后再通过下面的addResourceHandler来给 "/**"添加静态文件的映射
this.customizeResourceHandlerRegistration(registry.
addResourceHandler(new String[{staticPathPattern}).
addResourceLocations(WebMvcAutoConfiguration.
getResourceLocations(this.resourceProperties.getStaticLocations()))
} }
默认的静态资源文件夹:
"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径
也就是访问 “/” 相当于访问
“classpath:/META‐INF/resources/”;
“classpath:/resources/” ;
“classpath:/static/” ;
…
如: localhost:8080/abc.html -----> localhost:8080/static/abc.html
3. 欢迎页: “./index.html"被”/"映射;
所以:localhost:8080/ 相当于localhost:8080/static/index.html
2.thymeleaf自动配置的视图解析
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = Charset.forName("UTF‐8");
private static final MimeType DEFAULT_CONTENT_TYPE =MimeType.valueOf("text/html");
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html"; //
这里的 classpath:/templates/ 和 .html 相当于spring中视图解析器配置的
WEB-INF/pages 和 .jsp
都是给他设置前缀(prefix)和后缀(suffix)
所以 return “aaa” 就相当于 return “/templates/aaa.html”