Springboot的web開發中static和templates的區別
阿新 • • 發佈:2020-09-07
1.新建一個SpringBoot Maven專案
1.1 依賴設定(選中web和Thymeleaf)
1.2 resource結構如下
2. static目錄
2.1 在static下新建hello1.html
執行程式,瀏覽器輸入http://localhost:8080/hello1.html
so,可以在根目錄下訪問hello1.html,static目錄類似於傳統Java web中的webroot或webcontent
2.2 也可以通過介面跳轉
2.2.1 新增介面
@RequestMapping("hello1")
public String hello1() {
return "hello1.html";
}
- 1
- 2
- 3
- 4
- 5
2.2.2 註釋掉thymeleaf依賴
<dependencies>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-thymeleaf</artifactId>-->
<!--</dependency>-->
- 1
- 2
- 3
- 4
- 5
2.2.3 瀏覽器輸入http://localhost:8080/hello1
3. template目錄
3.1 在template下新建hello2.html
執行程式,瀏覽器輸入http://localhost:8080/hello2.html
templates下的動態頁面不能直接訪問
3.2 通過介面訪問
3.2.1 新增介面
注意介面中return的頁面不包含.html字尾
@RequestMapping("hello2")
public String hello2() {
return "hello2";
}
- 1
- 2
- 3
- 4
- 5
3.2.2 瀏覽器輸入http://localhost:8080/hello2
4.總結
靜態頁面的return預設是跳轉到/static/目錄下,當在pom.xml中引入了thymeleaf元件,動態跳轉會覆蓋預設的靜態跳轉,預設就會跳轉到/templates/下,注意看兩者return程式碼也有區別,動態沒有html字尾。
原文連結:https://blog.csdn.net/weixin_37891479/article/details/82117175