SpringBoot項目中訪問HTML頁面的三種方法
一.將 index.html 作為首頁面
1.靜態(tài)首頁
springboot項目啟動后,默認會到靜態(tài)資源 resources->static 目錄下查找index.html頁面
2.動態(tài)首頁
在靜態(tài)資源路徑找不到 index.html 文件,會到 resources->templates 目錄下找 index.html
二.使用自定義 xxx.html 作為首頁面
1.方法一:通過Controller控制首頁
@RestController public class IndexController { @RequestMapping("/") public String hello(){ System.out.println("OK"); return "test"; } }
三.用Controller控制層返回任意html頁面
1.在pom.xml文件中添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.書寫Controller層
注解要寫@controller而不是@restController,前者用來渲染頁面,后者用來返回數(shù)據(jù)
到此這篇關(guān)于SpringBoot項目中訪問HTML頁面的三種方法的文章就介紹到這了,更多相關(guān)SpringBoot訪問HTML頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Plus介紹及Spring Boot 3集成指南
本文介紹了MyBatis-Plus的基本特性及其與Spring Boot 3的集成步驟,通過使用MyBatis-Plus,開發(fā)者可以快速地搭建和開發(fā)數(shù)據(jù)訪問層,同時提高代碼質(zhì)量和開發(fā)效率,感興趣的朋友一起看看吧2024-05-05Springboot pom項目間接依賴包版本與預(yù)期不符原因解決分析
這篇文章主要介紹了Springboot pom項目間接依賴包版本與預(yù)期不符原因解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08前端如何調(diào)用后端接口進行數(shù)據(jù)交互詳解(axios和SpringBoot)
一般來講前端不會給后端接口,而是后端給前端接口的情況比較普遍,下面這篇文章主要給大家介紹了關(guān)于前端如何調(diào)用后端接口進行數(shù)據(jù)交互的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-03-03