(四)SpringBoot映射静态资源

技术/杨33

(四)SpringBoot映射静态资源

一、查看SpringBoot源代码,理解SpringBoot项目的静态资源应该放哪?

SpringBoot默认支持映射静态资源的功能。

静态资源包括图标、CSS、js、html等页面文件。

1、第一种资源:webjars,静态资源以jar包的形式引入

  • 有一个类WebMvcAutoConfiguration下面的方法addResourceHandlers,该方法负责静态资源的映射。
<code>

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(WebMvcAutoConfiguration.getResourceLocations(

this

.resourceProperties.getStaticLocations())).setCachePeriod(

this

.getSeconds(cachePeriod)).setCacheControl(cacheControl)); } } }/<code>

源码中有个匹配规则,所有"/webjars/**"的页面请求,都去项目的"
classpath:/META-INF/resources/webjars/"目录下找静态资源。

"
classpath:/META-INF/resources/webjars/"目录下放的是静态资源的jar包。那么就需要以maven的依赖方式引入静态资源的jar包。

  • webjars官网提供了静态资源的maven的依赖:https://www.webjars.org/
(四)SpringBoot映射静态资源

webjars官网

  • 将依赖代码粘贴到pom.xml文件中,如图:
(四)SpringBoot映射静态资源

引入jquery的webjar包

  • 最终项目引入的jar包目录,跟源代码的匹配规则指定的目录是一致的。
(四)SpringBoot映射静态资源

项目引入的jar包目录

  • 启动项目,浏览器访问下http://localhost:8083/webjars/jquery/3.5.0/jquery.js
(四)SpringBoot映射静态资源

成功访问到引入的jQuery

2、第二种资源:开发人员自己编写的静态资源文件

查看SpringBoot匹配规则,所有的静态资源需要放在项目的这些目录下的其中一个即可:

"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"

(四)SpringBoot映射静态资源

静态资源存放目录

  • "classpath:/static/"目录下新建toutiao.html页面
(四)SpringBoot映射静态资源

  • 启动项目,浏览器访问下http://localhost:8083/toutiao.html
(四)SpringBoot映射静态资源

成功打印页面内容

  • SpringBoot的默认欢迎页面就是index.html,浏览器直接访问ip和端口就可以跳转

http://localhost:8083/

(四)SpringBoot映射静态资源

3、自定义静态资源的存放文件夹

在application.yml中添加配置

<code>

spring

:

resources

:

static-locations

:

classpath

:/test//<code>

或者在application.properties添加配置

<code>

spring.resources.static-locations

=classpath:/test//<code>

配置完成后,再访问SpringBoot默认的路径就不通了。

(四)SpringBoot映射静态资源

需要将该html文件放到自定义的路径classpath:/test/下,才能访问。

二、html文件套用模板可以使用SpringBoot推荐的模板引擎Thymeleaf

thymeleaf模板语法简单,功能强大。

1、给项目导入Thymeleaf模板引擎依赖starter包

<code>

<

dependency

>

<

groupId

>

org.springframework.boot

groupId

>

<

artifactId

>

spring-boot-starter-thymeleaf

artifactId

>

dependency

>

/<code>


spring-boot-starter-parent

默认提供了thymeleaf的版本,所以最后导入的jar包版本就是3.0.11.RELEASE

(四)SpringBoot映射静态资源

spring-boot-starter-parent默认提供第三方jar包的版本

2、Thymeleaf模板的使用

SpringBoot项目加载Thymeleaf模板的规则是:

  • 文件夹满足classpath:/templates/
  • 文件名以.html结尾的
(四)SpringBoot映射静态资源

SpringBoot项目加载Thymeleaf模板的规则

html文件头部添加语法提示:xmlns:th="http://www.thymeleaf.org"


作者:杨33,北京互联网公司在职Java开发,专注分享写作干货。欢迎关注我,期待你的点赞评论。

Spring Boot系列知识,下章节内容更加有料。


分享到:


相關文章: