Springboot-thymeleaf如何用th:if做條件判斷


步驟 1 : 可運行項目

本知識點是建立在 上一個知識 點進行

首先下載一個簡單的可運行項目作為演示:網盤鏈接http://t.cn/A6AltBtG

下載后解壓,比如解壓到 E:\project\springboot 目錄下

步驟 2 : 修改 TestController

增加一個布爾值數據,並且放在 model 中便於視圖上獲取

package com.ryan.springboot.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ryan.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,"夢卻了無影蹤", 666);
        boolean testBoolean = true;
        
        m.addAttribute("htmlContent", htmlContent);
        m.addAttribute("currentProduct", currentProduct);
        m.addAttribute("testBoolean", testBoolean);
        
        return "test";
    }
}

步驟 3 : 修改 test.html

  1. Thymeleaf 的條件判斷是 通過 th:if 來做的,只有為真的時候,才會顯示當前元素
<p th:if="${testBoolean}" >如果 testBoolean 是 true ,本句話就會顯示</p>
  1. 取反可以用 not, 或者用 th:unless.
<p th:if="${not testBoolean}" >取反 ,所以如果 testBoolean 是 true ,本句話就不會顯示</p>
<p th:unless="${testBoolean}" >unless 等同於上一句,所以如果 testBoolean 是 true ,本句話就不會顯示</p>
  1. 除此之外,三元表達式也比較常見
<p th:text="${testBoolean}?'當 testBoolean 為真的時候,顯示本句話,這是用三相表達式做的':''" ></p>

完整 test.html

<!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}?'當 testBoolean 為真的時候,顯示本句話,這是用三相表達式做的':''" ></p>
</div>

<div class="showing">
	<h2>顯示 轉義和非轉義的 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>算數運算</h2>
	<p th:text="${currentProduct.price+222}" ></p>
</div>

<div class="showing">
    <div th:replace="include::footer1" ></div>
    <div th:replace="include::footer2(2020,2200)" ></div>
</div>

</body>
</html>

步驟 4 : 關於真假判斷

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

  1. boolean 類型並且值是 true, 返回 true
  2. 數值類型並且值不是 0, 返回 true
  3. 字符類型(Char)並且值不是 0, 返回 true
  4. String 類型並且值不是 "false", "off", "no", 返回 true
  5. 不是 boolean, 數值, 字符, String 的其他類型, 返回 true
  6. 值是 null, 返回 false

步驟 5 : 重啟測試

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

http://127.0.0.1:8080/thymeleaf/test

顯示效果:

更多關於 Springboot_thymeleaf_條件 詳細內容,點擊學習: http://t.cn/A6Ae7Bgh


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM