SpringBoot默认映射和thymeleaf视图解析

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”

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值