本篇文章為大家展示了在spring boot項目中如何實現使用thymeleaf實現頁面跳轉,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
前言
在學習springboot 之后想結合著html做個小demo,無奈一直沒掌握竅門,在多番的搜索和嘗試下終于找到了配置的方法,使用thymeleaf做事前端頁面模板,不能使用純html.
thymeleaf介紹
Thymeleaf是面向Web和獨立環境的現代服務器端Java模板引擎。
Thymeleaf的主要目標是為您的開發工作流程帶來優雅的自然模板 - 可以在瀏覽器中正確顯示HTML,還可以作為靜態原型工作,從而在開發團隊中進行更強大的協作。
使用Spring Framework的模塊,與您最喜愛的工具進行大量集成,以及插入自己的功能的能力,Thymeleaf是現代HTML5 JVM Web開發的理想選擇,盡管它可以做的更多。
實戰
項目結構
thymeleaf pom依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
模板頁面
注意使用模板作為頁面時候必須要把模板頁面放在templates文件夾下
index.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>demo</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h2>my thymeleaf indexpage</h2> <a href="/info/more" rel="external nofollow" >更多詳情</a> </body> </html>
controller
@Controller public class PageController { @RequestMapping("/page") public String page3(Model model){ model.addAttribute("userName","張三"); return "hello"; } @RequestMapping("info/more") public String page2(){ return "hello2"; } @RequestMapping("sys/index") public String page(){ return "sys/index"; } }
測試
點擊更多詳情
上述內容就是在spring boot項目中如何實現使用thymeleaf實現頁面跳轉,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。