1. 程式人生 > 程式設計 >深入淺析springboot中static和templates區別

深入淺析springboot中static和templates區別

靜態頁面的return預設是跳轉到/static/目錄下,當在pom.xml中引入了thymeleaf元件,動態跳轉會覆蓋預設的靜態跳轉,預設就會跳轉到/templates/下,注意看兩者return程式碼也有區別,動態沒有html字尾。

1.1 在static下新建hello1.html

執行程式,瀏覽器輸入http://localhost:8080/hello1.html

在這裡插入圖片描述

so,可以在根目錄下訪問hello1.html,static目錄類似於傳統Java web中的webroot或webcontent

1.2 也可以通過介面跳轉
1.2.1 新增介面

@RequestMapping("hello1")
public String hello1() {
  return "hello1.html";
}

1.2.2 註釋掉thymeleaf依賴

<dependencies>
  <!--<dependency>-->
    <!--<groupId>org.springframework.boot</groupId>-->
    <!--<artifactId>spring-boot-starter-thymeleaf</artifactId>-->
  <!--</dependency>-->

1.2.3 瀏覽器輸入http://localhost:8080/hello1

在這裡插入圖片描述

2.template目錄
2.1 在template下新建hello2.html
執行程式,瀏覽器輸入http://localhost:8080/hello2.html

在這裡插入圖片描述

templates下的動態頁面不能直接訪問

2.2 通過介面訪問
2.2.1 新增介面

注意介面中return的頁面不包含.html字尾

@RequestMapping("hello2")
public String hello2() {
  return "hello2";
}

2.2.2 瀏覽器輸入http://localhost:8080/hello2

在這裡插入圖片描述

3.結束語

靜態頁面的return預設是跳轉到/static/目錄下,當在pom.xml中引入了thymeleaf元件,動態跳轉會覆蓋預設的靜態跳轉,預設就會跳轉到/templates/下,注意看兩者return程式碼也有區別,動態沒有html字尾。

4.總結:

改bug需要用心,掉頭髮是不可避免滴!!!

到此這篇關於深入淺析springboot中static和templates區別的文章就介紹到這了,更多相關springboot static和templates區別內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!