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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

THYMELEAF 如何用TH:IF做条件判断

發(fā)布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 THYMELEAF 如何用TH:IF做条件判断 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

TestController

增加一個布爾值數(shù)據(jù),并且放在model中便于視圖上獲取

package com.how2java.springboot.web; import java.util.ArrayList; import java.util.Date; import java.util.List;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;import com.how2java.springboot.pojo.Product;@Controller public class TestController {@RequestMapping("/test")public String test(Model m) {String htmlContent = "<p style='color:red'> 紅色文字</p>";Product currentProduct =new Product(5,"product e", 200);boolean testBoolean = true;m.addAttribute("htmlContent", htmlContent);m.addAttribute("currentProduct", currentProduct);m.addAttribute("testBoolean", testBoolean);return "test";} }

步驟 5 :

test.html
Thymeleaf 的條件判斷是 通過 th:if 來做的,只有為真的時候,才會顯示當(dāng)前元素

如果testBoolean 是 true ,本句話就會顯示

取反可以用not, 或者用th:unless.

取反 ,所以如果testBoolean 是 true ,本句話就不會顯示

unless 等同于上一句,所以如果testBoolean 是 true ,本句話就不會顯示

除此之外,三元表達式也比較常見

<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head><title>hello</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" type="text/css" media="all" href="../../webapp/static/css/style.css"th:href="@{/static/css/style.css}"/><script type="text/javascript" src="../../webapp/static/js/thymeleaf.js" th:src="@{/static/js/thymeleaf.js}"></script><style>h2{text-decoration: underline;font-size:0.9em;color:gray;}</style> </head> <body><div class="showing"><h2>條件判斷</h2><p th:if="${testBoolean}" >如果testBoolean 是 true ,本句話就會顯示</p><p th:if="${not testBoolean}" >取反 ,所以如果testBoolean 是 true ,本句話就不會顯示</p><p th:unless="${testBoolean}" >unless 等同于上一句,所以如果testBoolean 是 true ,本句話就不會顯示</p><p th:text="${testBoolean}?'當(dāng)testBoolean為真的時候,顯示本句話,這是用三相表達式做的':''" ></p> </div><div class="showing"><h2>顯示 轉(zhuǎn)義和非轉(zhuǎn)義的 html 文本</h2><p th:text="${htmlContent}" ></p><p th:utext="${htmlContent}" ></p> </div><div class="showing"><h2>顯示對象以及對象屬性</h2><p th:text="${currentProduct}" ></p><p th:text="${currentProduct.name}" ></p><p th:text="${currentProduct.getName()}" ></p> </div><div class="showing" th:object="${currentProduct}"><h2>*{}方式顯示屬性</h2><p th:text="*{name}" ></p> </div><div class="showing"><h2>算數(shù)運算</h2><p th:text="${currentProduct.price+999}" ></p> </div><div class="showing"><div th:replace="include::footer1" ></div><div th:replace="include::footer2(2015,2018)" ></div> </div></body></html>

步驟 6 :

關(guān)于真假判斷

不只是布爾值的 true 和 false, th:if 表達式返回其他值時也會被認(rèn)為是 true 或 false,規(guī)則如下:

boolean 類型并且值是 true, 返回 true
數(shù)值類型并且值不是 0, 返回 true
字符類型(Char)并且值不是 0, 返回 true
String 類型并且值不是 “false”, “off”, “no”, 返回 true
不是 boolean, 數(shù)值, 字符, String 的其他類型, 返回 true

值是 null, 返回 false
步驟 7 :

重啟測試

重新啟動Application.java, 然后訪問如下地址測試:

http://127.0.0.1:8080/thymeleaf/test

即可看到如圖所示的效果。

總結(jié)

以上是生活随笔為你收集整理的THYMELEAF 如何用TH:IF做条件判断的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。