聊聊如何在springboot中添加模版
添加依賴
案例在之前項目 Spring Boot 整合 Swagger 接口文檔工具 基礎上進行整改~
這里我們添加模版依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
添加模版
我們添加測試的模版。
在 resources/templates
文件夾下新建 admin/index.html
文件:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Index</title> </head> <body> <h1>Index</h1> <h1 th:text="${message}"></h1> </body> </html>
這里我們簡單設置模板,message
假設是數(shù)據(jù)庫返回的數(shù)據(jù)~
添加映射
我們緊接著編寫 Controller
:
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("admin") public class SysUserController { @RequestMapping("/index") public String Index(Model model) { model.addAttribute("message", "Hello, Spring Boot Thymeleaf!"); return "admin/index"; } }
我們指定了路由的訪問路徑為 admin/index
,然后其對應的模版文件是 resources
下面的 admin/index.html
文件,上面代碼映射的模版路徑 return "admin/index"
。
效果
完整操作后,啟動項目,可得到下面的結果。
當然,頁面很丑,但是不影響我們對該知識點的吸收。
感謝閱讀~
到此這篇關于聊聊如何在springboot中添加模版的文章就介紹到這了,更多相關springboot添加模版內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于SpringBoot集成測試遠程連接Redis服務的教程詳解
這篇文章主要介紹了基于SpringBoot集成測試遠程連接的Redis服務的相關知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03springboot如何讀取自定義properties并注入到bean中
這篇文章主要介紹了springboot讀取自定義properties并注入到bean中,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Spring mvc Controller和RestFul原理解析
這篇文章主要介紹了Spring mvc Controller和RestFul原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03