前言
在學習springboot 之后想結合著html做個小demo,無奈一直沒掌握竅門,在多番的搜索和嘗試下終于找到了配置的方法,使用thymeleaf做事前端頁面模板,不能使用純html.
thymeleaf介紹
Thymeleaf是面向Web和獨立環境的現代服務器端Java模板引擎。
Thymeleaf的主要目標是為您的開發工作流程帶來優雅的自然模板 - 可以在瀏覽器中正確顯示HTML,還可以作為靜態原型工作,從而在開發團隊中進行更強大的協作。
使用Spring Framework的模塊,與您最喜愛的工具進行大量集成,以及插入自己的功能的能力,Thymeleaf是現代HTML5 JVM Web開發的理想選擇,盡管它可以做的更多。
實戰
項目結構
thymeleaf pom依賴
1
2
3
4
5
6
7
8
9
|
< 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
1
2
3
4
5
6
7
8
9
10
11
|
<!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 > < h1 >my thymeleaf indexpage</ h1 > < a href = "/info/more" rel = "external nofollow" >更多詳情</ a > </ body > </ html > |
controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@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" ; } } |
測試
點擊更多詳情
項目源碼: github地址
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.tuicool.com/articles/rU7RRbJ