詳解SpringBoot+Thymeleaf 基于HTML5的現(xiàn)代模板引擎
序言:
Thymeleaf 是Java服務(wù)端的模板引擎,與傳統(tǒng)的JSP不同,前者可以使用瀏覽器直接打開,因為可以忽略掉拓展屬性,相當(dāng)于打開原生頁面,給前端人員也帶來一定的便利。如果你已經(jīng)厭倦了JSP+JSTL的組合,Thymeleaf或許是個不錯的選擇!本工程傳送門:SpringBoot-Web-Thymeleaf
開始使用
1.引入依賴
SpringBoot默認(rèn)提供了Thymeleaf的Starter,只需簡單引入依賴即可。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
目前默認(rèn)版本是2.1,如果想升級版本到3.0,可以這樣修改。
<properties>
<thymeleaf.version>3.0.7.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
</properties>
為了方便開發(fā),項目案例采用了熱部署工具dev-tools ,這樣我們在修改頁面之后,IDEA會自動加載,從而達(dá)到實現(xiàn)熱更新的效果。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
注:由于IDEA默認(rèn)關(guān)閉了熱部署,需要做一些設(shè)置才能使其生效。解決方法:首先按住Ctrl+Shift+Alt+/ 然后進入 Registry ,然后勾選compiler.automake.allow.when.app.running 即可。另外,Build->Compiler 也要勾選上Build Project automatically .
2. 添加相關(guān)配置
Thymeleaf默認(rèn)開啟了頁面緩存,在開發(fā)的時候,應(yīng)該關(guān)閉緩存。此外,通常我們還會指定頁面的存放路徑。(默認(rèn)是classpath:/templates/)
application.yml 配置如下: spring: thymeleaf: cache: false #關(guān)閉緩存 prefix: classpath:/views/ #添加路徑前綴
3.編寫HTML
編寫Thymeleaf和書寫HTML5頁面沒有什么不同,最大的轉(zhuǎn)變就是使用拓展屬性(th:xx)去跟服務(wù)端進行數(shù)據(jù)交互,保留原始頁面風(fēng)格,也是Thymeleaf的推崇的風(fēng)格。例如下面這個簡單的案例,啟動項目,我們發(fā)現(xiàn)頁面跳轉(zhuǎn)的是簡書的連接,這一點也驗證了Thymeleaf覆蓋原生頁面數(shù)據(jù)的極佳能力。
頁面代碼:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf</title>
</head>
<body>
<h2>歡迎使用Thymeleaf!!</h2>
<a rel="external nofollow" th:href="${info}" rel="external nofollow" >戳我有驚喜</a>
</body>
</html>
后端代碼:
@Controller
public class UserController {
@GetMapping("/")
public String index(Model model) {
model.addAttribute("info", "user/list");
return "index";
}
@GetMapping("/user")
public String hehe(Model model) {
model.addAttribute("user", new User(UUID.randomUUID().toString(), "yizhiwazi", "20170928"));
return "user";
}
@GetMapping("/user/list")
public String userlist(Model model) {
List<User> userList = new ArrayList<>();
userList.add(new User(UUID.randomUUID().toString(), "yizhiwazi", "20170928"));
userList.add(new User(UUID.randomUUID().toString(), "kumamon", "123456"));
userList.add(new User(UUID.randomUUID().toString(), "admin", "admin"));
model.addAttribute("userList", userList);
return "userList";
}
}
現(xiàn)在我們嘗試回填一個表單,展示單個用戶信息。
<!-- 使用th:object 搭配*{} 可以省略對象名 -->
<form action="/" th:object="${user}" >
<input type="hidden" name="userId" th:value="*{userId}" />
<input type="text" name="username" th:value="*{username}" />
<input type="text" name="password" th:value="*{password}" />
</form>
接下來,我們進入一個更復(fù)雜的案例,例如展示一個用戶列表信息,不需要編寫新的標(biāo)簽,就可以完成對批量用戶的遍歷。
<h2>用戶列表</h2>
<!--說明: th:each="obj,stat:${objects}" 分別代表單個實例,狀態(tài)(可省略),待遍歷對象-->
<div th:each="user:${userList}">
<input type="hidden" name="userId" th:value="${user.userId}"/>
用戶姓名:<input type="text" name="password" th:value="${user.username}"/>
登錄密碼:<input type="text" name="username" th:value="${user.password}"/>
</div>
好了,Thymeleaf簡單介紹到這里,更多詳細(xì)說明,可閱讀Thymeleaf 官方指南 3.0
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot學(xué)習(xí)之Thymeleaf模板引擎及原理介紹
- springboot?使用clickhouse實時大數(shù)據(jù)分析引擎(使用方式)
- SpringBoot整合Drools規(guī)則引擎動態(tài)生成業(yè)務(wù)規(guī)則的實現(xiàn)
- springboot2.5.2與 flowable6.6.0整合流程引擎應(yīng)用分析
- SpringBoot使用Thymeleaf模板引擎訪問靜態(tài)html的過程
- SpringBoot2整合Drools規(guī)則引擎及案例詳解
- 詳解Elastic Search搜索引擎在SpringBoot中的實踐
- springboot?整合表達(dá)式計算引擎?Aviator?使用示例詳解
相關(guān)文章
springboot打包部署到linux服務(wù)器的方法
這篇文章主要介紹了springboot打包部署到linux服務(wù)器的方法,通過實例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06
SSH框架網(wǎng)上商城項目第25戰(zhàn)之使用java email給用戶發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項目第25戰(zhàn)之使用java email給用戶發(fā)送郵件,感興趣的小伙伴們可以參考一下2016-06-06
java使用webuploader實現(xiàn)跨域上傳詳解
目前初步接觸JAVA圖片上傳,用的webuploader。已經(jīng)跟后臺對接上,但是有個問題就是跨域請求,通過查找相關(guān)資料終于實現(xiàn)了,下面這篇文章主要給大家介紹了關(guān)于java使用webuploader實現(xiàn)跨域上傳的相關(guān)資料,需要的朋友可以參考下。2017-07-07
SpringBoot validator參數(shù)驗證restful自定義錯誤碼響應(yīng)方式
這篇文章主要介紹了SpringBoot validator參數(shù)驗證restful自定義錯誤碼響應(yīng)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

