日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Thymeleaf 基本用法总结

發布時間:2025/5/22 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Thymeleaf 基本用法总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?一、引用命名空間?<html xmlns:th="http://www.thymeleaf.org">?

? ? ? ? 在html中引入此命名空間,可避免編輯器出現html驗證錯誤,雖然加不加命名空間對Thymeleaf的功能沒有任何影響。

?

二、輸出內容

? ? ? ? 2.1 ?<p th:text="#{home.welcome}">Welcome to our grocery store!</p>

? ? ? ? 說明:

? ? ? ? ? ? ? ? ?1. th:text ?用來將內容輸出到所在標簽的body中。

? ? ? ? ? ? ? ? ?2.?#{home.welcome} 用來引入數據home對象中的 welcome屬性。

? ? ? ? ? ? ? ? ?3. 可以用th:utext 用來顯示“unescaped ” 的html內容。

? ? ? ? 2.2 ???<p>Today is: <span th:text="${today}">13 February 2011</span></p>

? ? ? ? 說明:${today} 用來引用 today 變量

三、訪問對象? ? ??

? ? ? ?${param.x} 返回名為x 的 request參數。(可能有多個值)

? ? ? ?${session.x} 返回名為x的Session參數。

? ? ? ?${application.x} 返回名為 servlet context 的參數。

? ? ?

四、基本語法

? ? ? ?4.1 ?#{home.welcome} -- ?訪問數據

? ? ? ?4.2 ?#{home.welcome(${session.user.name})} ?-- 格式化數據 當 home.welcome 為 "abcdegf{0}" ?類似這種內容時。(多個參數以逗句分隔)。

? ? ? ?4.3 ?${today} --- 訪問變量

? ? ? ?4.4 ?訪問基本對象

#ctx: the context object.
#vars: the context variables.
#locale: the context locale.
#request: (only in Web Contexts) the HttpServletRequest object.
#response: (only in Web Contexts) the HttpServletResponse object.
#session: (only in Web Contexts) the HttpSession object.
#servletContext: (only in Web Contexts) the ServletContext object.

其它公共對象參考:?http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-a-expression-basic-objects

? ? ? ? 4.5 ?日期的輸出

? ? ? ??<span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span>

? ? ? ? 4.6 ?星號語法

<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

4.7 ?輸出URL

? ? ? ?<a href="product/list.html" th:href="@{/product/list}">Product List</a>

? ? ? ?<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>

? ? ? ?4.8 ?使用代碼段

? ? ? ?<div th:insert="~{commons :: main}">...</div>

? ? ? ?4.9 ?直接輸出內容? ?

<span th:text="'working web application'"> -- 輸出字符

<span th:text="2013 + 2"> ?-- 輸出數據表達式

<div th:if="${user.isAdmin()} == false"> ?--輸出布爾表達式

<span th:text="'Welcome to our application, ' + ${user.name} + '!'"> -- 帶變量的

4.10 條件表達式

<tr th:class="${row.even}? 'even' : 'odd'">
... ?
</tr>

<tr th:class="${row.even}? 'alt'">
...省略 false 結果的表達方式
</tr>

<div th:object="${session.user}">
...省略 true 結果的表達方式
<p>Age: <span th:text="*{age}?: '(no age specified)'">27</span>.</p>
</div>

<span th:text="${user.name} ?: _">no user authenticated</span> --不做任何處理時用下劃線 _ 表示

4.11 ?格式化?

? ? ? ?<td th:text="${{user.lastAccessDate}}">...</td> --${{.}} ?調用默認的格式化器來輸出結果。

? ? ? ?4.12 ?預處理

? ? ? ?<p th:text="${__#{article.text('textVar')}__}">Some text here...</p> ?

? ? ? ?說明:thymeleaf 的處理模板內容的順序與書寫順序無關,只能通過 ?__${expression}__ ,來將需要先一步計算出來后面 ? ? ? ? ?要用的變量指定為優化處理。

?

?五、設置 Attribute 值

? ? ? ?5.1 設置任何Attribute 的方法

? ? ? ?<input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/> ? --設置單個

? ? ? ?<img src="../../images/gtvglogo.png" ?th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" /> ?--一次設置多個

? ? ? ? 5.2 設置一些內置的Attribute的方法? ?

? ? ? ?<li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li>

? ? ? ?<form action="subscribe.html" th:action="@{/subscribe}">

? ? ? ?<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>

? ? ? ?<img src="../../images/gtvglogo.png" ?th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" /> -- 一次設置多個(alt title)的方法

? ? ? ?其它的可用屬性:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes

? ? ? ? 5.3 設置html里沒有指的任何屬性的語法

? ? ? ??<span th:whatever="${user.name}">...</span> ? ---whatever 可以換成任何你想設的屬性

?

六、循環輸出的語法

? ? ? ?6.1 基本循環

<tr th:each="prod : ${prods}">

? ? ?<td th:text="${prod.name}">Onions</td>
? ? ?<td th:text="${prod.price}">2.41</td>
? ? ?<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>

6.2 循環狀態的使用

<table>
<tr>
<th>NAME</th>
<th>PRICE</th>
<th>IN STOCK</th>
</tr>
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
</table>

? ? ? ?關于狀態的其它信息的使用詳細參考:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#keeping-iteration-status

? ? ? ?

七、條件判斷

? ? ? ?7.1 if 和 unless

? ? ? ?<a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">view</a>

? ? ? ?<a href="comments.html" ?th:href="@{/product/comments(prodId=${prod.id})}" ? th:if="${not #lists.isEmpty(prod.comments)}">view</a>

? ? ? ?7.2 switch 語句

<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p> ? ?--默認的 case 相當于default
</div>

?

八、模板 include

? ? ? 8.1 定義和引用代碼塊

? ? ? 定義代碼塊

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<body>

<div th:fragment="copy">
&copy; 2011 The Good Thymes Virtual Grocery
</div>

</body>

</html>

引用代碼塊

<body>

...

<div th:insert="~{footer :: copy}"></div>

</body>

引用未用fragment 標注的代碼塊?

<div id="copy-section">
&copy; 2011 The Good Thymes Virtual Grocery
</div>

<body>

...

<div th:insert="~{footer :: #copy-section}"></div>

</body>

8.2?th:insert th:replace th:include 之間的區別

th:insert ?--- 插入代碼塊 ? ?th:replace -- 替換代碼塊會替換掉容器標簽 ? th:include ---- 和insert相似但只會插入fragment標注body內的內容。

8.3 ?帶參數的代碼段

<div th:fragment="frag (onevar,twovar)">
<p th:text="${onevar} + ' - ' + ${twovar}">...</p>
</div>

? ? ?<div th:replace="::frag (${value1},${value2})">...</div>
? ? ?<div th:replace="::frag (onevar=${value1},twovar=${value2})">...</div>

?

九、局部變量的使用示例

<div th:with="firstPer=${persons[0]}">
<p>
The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.
</p>
</div>

<div th:with="firstPer=${persons[0]},secondPer=${persons[1]}">
<p>
The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.
</p>
<p>
But the name of the second person is?
<span th:text="${secondPer.name}">Marcus Antonius</span>.
</p>
</div>

十、注釋

? ? ? ??<!-- ... --> ?

十一、說明

? ? ? ? 以上只列出Thymeleaf了簡要常用的語法和使用方式,更多詳情的說明和規則請參見:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf

總結

以上是生活随笔為你收集整理的Thymeleaf 基本用法总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 在线伊人网 | 熟妇人妻精品一区二区三区视频 | 日本新japanese乱熟 | 六月丁香婷婷激情 | 久久9久久| 三上悠亚激情av一区二区三区 | 中文字幕一区日韩 | 波多野结衣之双调教hd | 日韩小视频在线观看 | 免费成人深夜在线观看 | 欧美激情三区 | 99re这里| 激情网五月 | 中文字幕一区二区在线视频 | av免费观看网址 | av的天堂| 亚洲色图视频在线 | 国模少妇一区二区三区 | 在线观看1区 | 后进极品美女圆润翘臀 | 四虎国产精品免费 | 五月婷婷丁香久久 | 蜜臀av性久久久久蜜臀aⅴ涩爱 | 日韩av大片 | 精品一区二区三区电影 | 两个女人互添下身爱爱 | 亲切的金子片段 | 亚洲综合成人在线 | 色播在线观看 | 99精品乱码国产在线观看 | 成年人午夜免费视频 | 国产一区二区三区色淫影院 | 69久久精品无码一区二区 | 亚洲男女激情 | 91天天爽| 国产又黄又粗又爽 | 中文字幕av无码一区二区三区 | 欧美播放| 午夜国产 | 国产精品99久久久久久大便 | 成人一级视频 | 操女人免费视频 | 久久精彩 | 精品国产一区在线 | 99这里只有 | 99久久精品国产一区二区成人 | 玩偶姐姐在线观看免费 | 日韩网红少妇无码视频香港 | 国产视频手机在线观看 | 狠狠躁日日躁夜夜躁av | 不卡免费av | av午夜在线观看 | 成年人性视频 | 久久久高清免费视频 | 五十路熟母 | 丁香八月婷婷 | 黄色片在线视频 | 人人超碰97| 欧美日韩午夜精品 | 久草热视频| 午夜在线观看av | 人妻无码一区二区三区 | 欧美涩涩视频 | 国产福利午夜 | 成人性生交大片免费看中文 | jizz久久| 日韩一片 | 日韩欧美少妇 | 成人免费看片' | 欧美成人三级视频 | 污污视频在线免费观看 | 亚洲免费成人网 | 久久99国产精品久久99果冻传媒 | 在线看片你懂得 | 天天看夜夜看 | 日韩国产片 | 国产精品后入内射日本在线观看 | 国产精品96久久久久久 | 久久99精品波多结衣一区 | 葵司免费一区二区三区四区五区 | 老子影院午夜精品无码 | 在线精品免费视频 | 色欲久久久天天天精品综合网 | 欧美激情成人在线 | 国产精品综合久久 | 成人三级黄色 | 免费污片网站 | 97精品人妻一区二区三区蜜桃 | 久久99伊人| 欧美性白人极品1819hd | 原创真实夫妻啪啪av | 手机看黄色 | 人妻久久一区二区三区 | 国产操片| 精品国产一区二区三区四 | 床戏高潮做进去大尺度视频 | 欧美草比视频 | 日本高清在线一区 | 欧美又粗又大xxxxbbbb疯狂 |