(四)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系列知識,下章節內容更加有料。


分享到:


相關文章: