`

13. 处理静态资源(默认资源映射)【从零开始学Spring Boot】

阅读更多

 à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

 

 

Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。

建议大家使用Spring Boot的默认配置方式,如果需要特殊处理的再通过配置进行修改。

如果想要自己完全控制WebMVC,就需要在@Configuration注解的配置类上增加@EnableWebMvc@SpringBootApplication 注解的程序入口类已经包含@Configuration),增加该注解以后WebMvcAutoConfiguration中配置就不会生效,你需要自己来配置需要的每一项。这种情况下的配置还是要多看一下WebMvcAutoConfiguration类。

我们既然是快速使用Spring Boot,并不想过多的自己再重新配置。本文还是主要针对Spring Boot的默认处理方式,部分配置在application 配置文件中(.properties .yml

 

默认资源映射

我们在启动应用的时候,可以在控制台中看到如下信息:

2016-01-08 09:29:30.362  INFO 24932 ---[           main]o.s.w.s.handler.SimpleUrlHandlerMapping  : MappedURLpath[/webjars/**]ontohandleroftype[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-08 09:29:30.362  INFO 24932 ---[           main]o.s.w.s.handler.SimpleUrlHandlerMapping  : MappedURLpath[/**]ontohandleroftype[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-08 09:29:30.437  INFO 24932 ---[           main]o.s.w.s.handler.SimpleUrlHandlerMapping  : MappedURLpath[/**/favicon.ico]ont

其中默认配置的 /** 映射到 /static (或/public/resources/META-INF/resources 
其中默认配置的 /webjars/** 映射到 classpath:/META-INF/resources/webjars/ 
PS
:上面的 staticpublicresources 等目录都在 classpath: 下面(如 src/main/resources/static)。

如果我按如下结构存放相同名称的图片,那么Spring Boot 读取图片的优先级是怎样的呢? 

如下图: 


 

当我们访问地址 http://localhost:8080/test.jpg 的时候,显示哪张图片?这里可以直接告诉大家,优先级顺序为:META/resources > resources > static > public  (已进行测试)

如果我们想访问test2.jpg,请求地址 http://localhost:8080/img/test2.jpg

 

视频&交流平台

à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

 

网易云课堂视频最新更新

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

3、Spring Boot日志-log4j2

第十二章 Spring Boot 知识点2

1、spring boot 服务配置和部署

2、Spring Boot 定制URL匹配规则

 

 

 

 

 

  • 大小: 6.7 KB
分享到:
评论
4 楼 颖宝blingbling 2017-03-05  
[flash=200,200][/flash]              op                  s:
3 楼 yuchao2015 2016-12-20  
多张同名,不同图片即可测试。   这个可怕
2 楼 林祥纤 2016-08-20  
bolo 写道
我想问下博主,优先级顺序为:META/resources > resources > static > public  (已进行测试),这个测试是怎么测的?


多张同名,不同图片即可测试。
1 楼 bolo 2016-08-19  
我想问下博主,优先级顺序为:META/resources > resources > static > public  (已进行测试),这个测试是怎么测的?

相关推荐

Global site tag (gtag.js) - Google Analytics