Springboot-thymeleaf如何⽤th:if做条件判断步骤 1 : 可运⾏项⽬
本知识点是建⽴在点进⾏
下载后解压,⽐如解压到 E:\project\springboot ⽬录下
步骤 2 : 修改 TestController
增加⼀个布尔值数据,并且放在 model 中便于视图上获取
an.springboot.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
an.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>
2. 取反可以⽤ not, 或者⽤ th:unless.
<p th:if="${not testBoolean}" >取反,所以如果 testBoolean 是 true ,本句话就不会显⽰</p>
<p th:unless="${testBoolean}" >unless 等同于上⼀句,所以如果 testBoolean 是 true ,本句话就不会显⽰</p>
3. 除此之外,三元表达式也⽐较常见
<p th:text="${testBoolean}?'当 testBoolean 为真的时候,显⽰本句话,这是⽤三相表达式做的':''" ></p>
完整 test.html
<!DOCTYPE HTML>
<html xmlns:th="">
<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="${Name()}" ></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>
thyme</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, 然后访问如下地址测试:
显⽰效果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论