Thymeleaf筆記

簡(jiǎn)單使用

引入標(biāo)簽

<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

標(biāo)簽的使用

$符號(hào)的使用

<td th:text="${users.email}"></td>

@符號(hào)的使用

<link th:href="@{/css/style.css}" rel="stylesheet" type="text/css"/>
<img th:src="@{/img/logo.png}">

each標(biāo)簽的使用

<div class="goodsItem" th:each="goods,goodsStat : ${goodsList}">
         <a th:href="@{'/goodDetail/'+${goods.id}}">
                   <img th:src="${goods.picUrl}" th:alt="${goods.goodsName}" class="goodsimg">
         </a><br/>
          <p class="f1">
                    <a th:href="@{'/goodDetail/'+${goods.id}}" th:text="${goods.goodsName}"></a>
          </p>
          <div class="f1" > ¥   <a th:text="${goods.price}"></a>       元 </div>
</div>

goodsStat 稱作狀態(tài)變量,屬性有:

index:當(dāng)前迭代對(duì)象的index(從0開(kāi)始計(jì)算)
count: 當(dāng)前迭代對(duì)象的index(從1開(kāi)始計(jì)算)
size:被迭代對(duì)象的大小
current:當(dāng)前迭代變量
even/odd:布爾值,當(dāng)前循環(huán)是否是偶數(shù)/奇數(shù)(從0開(kāi)始計(jì)算)
first:布爾值,當(dāng)前循環(huán)是否是第一個(gè)
last:布爾值,當(dāng)前循環(huán)是否是最后一個(gè)

*符號(hào)的使用

用一個(gè)預(yù)先選擇的對(duì)象來(lái)代替上下文變量容器(map)來(lái)執(zhí)行

<div class="goodsItem" th:each="goods,goodsStat : ${goodsList}">
  <div class="f1" > ¥   <a th:text="*{price}"></a>       元 </div>
<div th:object="${book}">
  ...
  <span th:text="*{title}">...</span>
  ...
</div>

if與unless標(biāo)簽的使用

<a href="#" th:if="${goods.type} == 0" th:class="searchSelected">大師咖啡</a>
<a href="#" th:unless="${goods.type} == 0" >大師咖啡</a>

多元運(yùn)算符的使用

<a href="#" th:class=" @{(${goods.type} ==0 ? 'searchSelected' : '')}>大師咖啡</a>
<a href="#" th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''">大師咖啡</a>

日期的格式化

 <td th:text="${#dates.format(good.createDate,'yyyy-MM-dd HH:mm:ss')}"></td>

標(biāo)簽內(nèi)函數(shù)傳參

<a class="red" th:onclick="'goodDelete(\''+${good.id}+'\')'">

引入模塊

<html xmlns:th="http://www.thymeleaf.org">
<div th:fragment="copy">
    <div id="footer">
        <div class="text">
            ? 20018-2020 SithCait 版權(quán)所有,并保留所有權(quán)利。<br/>
        </div>
    </div>
</div>
</html>
<div th:include="common/footer :: copy"></div>

JS中的內(nèi)聯(lián)文本

var username = [[${sesion.user.name}]];
var size = [[${size}]];

取整保留小數(shù)

<p>formatDecimal 整數(shù)部分隨意,小數(shù)點(diǎn)后保留兩位,四舍五入: <span th:text="${#numbers.formatDecimal(itdragonNum, 0, 2)}"/></p>
        <p>formatDecimal 整數(shù)部分保留五位數(shù),小數(shù)點(diǎn)后保留兩位,四舍五入: <span th:text="${#numbers.formatDecimal(itdragonNum, 5, 2)}"/></p>

JS中保留兩位小數(shù)

    //保留兩位小數(shù)
    function twoDecimal(a) {
        var b = parseFloat(a).toFixed(3);
        var c = b.substring(0,b.toString().length - 1);
        return c;
    }   

switch和case的使用

<td align="center" th:switch="${order.status}">
      <a th:case="'0'">待支付</a>
      <a th:case="'1'">已支付</a>
      <a th:case="'2'">未發(fā)貨</a>
      <a th:case="'3'">已發(fā)貨</a>
      <a th:case="'4'">確認(rèn)收貨</a>
      <a th:case="'5'">已取消</a>
 </td>

常用標(biāo)簽的使用:https://www.cnblogs.com/ityouknow/p/5833560.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容