springboot学习日志(二)-- thymeleaf学习
生活随笔
收集整理的這篇文章主要介紹了
springboot学习日志(二)-- thymeleaf学习
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本次學(xué)習(xí)如何使用thymeleaf以及相關(guān)語(yǔ)法
1、在上一章寫(xiě)的那樣 引入jar包到maven工程
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、同理配置yml
### springboot配置
spring:
##模板設(shè)置
thymeleaf:
prefix: classpath:/templates
suffix: .html
mode: LEGACYHTML5
encoding: utf-8
servlet:
content-type: text/html
cache: false
3、在需要引用thymeleaf添加引用頭
<html xmlns:th="http://www.thymeleaf.org">
下面記錄一下thymeleaf的模板語(yǔ)法 和jsp稍微有些出入 不過(guò)好在不需要修改文件類(lèi)型 直接將html進(jìn)行頭部引用就可以使用
(1)標(biāo)簽引入路徑或地址
<a th:href="@{http://www.baidu.com}"></a> //絕對(duì)路徑進(jìn)行訪問(wèn)
<script th:src="@{/}"></script>//相對(duì)路徑進(jìn)行訪問(wèn)
<link rel="stylesheet" th:href="@{css/base.css}">//默認(rèn)訪問(wèn)static下的文件夾
<img th:src="@{}"/>//圖片路徑引用
(2)使用變量動(dòng)態(tài)替換
<div th:text="hello ${roms}">hello world</div>
使用spring想也面?zhèn)髦祌oms:xxx即可在頁(yè)面匯總替換${roms}進(jìn)行內(nèi)容修改
需要注意的是 th:text 會(huì)替換掉標(biāo)簽內(nèi)的所有內(nèi)容
(3)條件適用
//使用if進(jìn)行判斷是否為真
<div th:text="hello world" th:if=${flag != true}>Login</div>
//使用unless 表示除外
<div th:text="hello world" th:unless=${flag != true}>Login</div>
(4)循環(huán)的使用
<table>
<tr th:each="list: ${list}">
<td th:text="${list.id}">1122334</td>
<td th:text="${plistod.name}">tony</td>
</tr>
</table>
(5)工具方法使用
//日期格式化
${#dates.format(date, 'yyyy/MMM/dd HH:mm')}
//當(dāng)前時(shí)間
${#dates.createNow()}
//當(dāng)前日期
${#dates.createToday()}
還有其他的工具類(lèi)#Calendars,#numbers,#strings,#objects,#bools,#arrays,#lists,#sets,#maps,#aggregates,#messages,#ids
詳細(xì)的api文檔可以查看官網(wǎng)
http://www.thymeleaf.org/doc/...
總結(jié)
以上是生活随笔為你收集整理的springboot学习日志(二)-- thymeleaf学习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ingress controller 和
- 下一篇: laravel 中 同个主域名下,多个项