Thymeleaf常用语法:条件判断 if、switch case
if语句
条件判断使用th:if,它会判断表达式是否成立,表达式的结果支持boolean、number、character、String及其他类型。
满足下面情况,if语句成立:
(1) 表达式的结果是数字且不是0
(2) 表达式的结果是字符串且不是false、off、no、0
(3) 表达式的结果是其他数据类型
switch case语句
(1) 类似Java的switch case语句:th:switch、th:case
(2) 使用th:case="*"来表示默认值
(3) 如果第一个th:case结果为true,则其它所有的th:case会被设置为false,即使它们的结果也是true
开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8
新建一个名称为demo的Spring Boot项目。
1、pom.xml
加入Thymeleaf依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、src/main/java/com/example/demo/TestController.java
package com.example.demo; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList;
import java.util.List; @Controller
public class TestController {
@RequestMapping("/")
public String test(Model model){
List<Integer> list = new ArrayList<Integer>();
list.add(1);
model.addAttribute("list", list);
model.addAttribute("flag", true);
model.addAttribute("gender", 1);
return "test";
}
}
3、src/main/resources/templates/test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h4>一、条件判断</h4>
[1]<div th:text="true" th:if="${flag}"></div>
[2]<div th:text="'数字,非0'" th:if="10"></div>
[3]<div th:text="'字符串,非false、off、no'" th:if="'a'"></div>
[4]<div th:text="其他数据类型" th:if="${list}"></div>
[5]<div th:text="字符串0" th:if="'0'"></div>
[6]<div th:text="数字0" th:if="0"></div>
[7]<div th:text="false" th:if="'false'"></div>
[8]<div th:text="off" th:if="'off'"></div>
[9]<div th:text="no" th:if="'no'"></div>
<h4>二、switch case</h4>
<select th:switch="${gender}">
<option th:case="1">男</option>
<option th:case="0">女</option>
<option th:case="*">其他</option>
</select> <select th:switch="2">
<option th:case="1">男</option>
<option th:case="0">女</option>
<option th:case="*">其他</option>
</select> </body>
</html>
浏览器访问:http://localhost:8080
右键查看网页源代码,生成的HTML源码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h4>一、条件判断</h4>
[1]<div>true</div>
[2]<div>数字,非0</div>
[3]<div>字符串,非false、off、no</div>
[4]<div>其他数据类型</div>
[5]<div>字符串0</div>
[6]
[7]
[8]
[9]
<h4>二、switch case</h4>
<select>
<option>男</option> </select> <select> <option>其他</option>
</select> </body>
</html>
Thymeleaf常用语法:条件判断 if、switch case的更多相关文章
- 为什么说在使用多条件判断时switch case语句比if语句效率高?
在学习JavaScript中的if控制语句和switch控制语句的时候,提到了使用多条件判断时switch case语句比if语句效率高,但是身为小白的我并没有在代码中看出有什么不同.去度娘找了半个小 ...
- shell编程 条件判断式----利用 case ..... esac 判断
条件判断式----利用 case ..... esac 判断 case $变量名称 in <==关键词为 case ,还有变量前有钱字号 "第一个变量内容") &l ...
- if判断和switch case 和三元运算符整理
if判断和switch case 和三元运算符整理 例子1:if判断写法: <script type="text/javascript"> var num = 12; ...
- linux Shell中常用的条件判断
linux Shell中常用的条件判断 -b file 若文件存在且是一个块特殊文件,则为真 -c file 若文件存在且是一个字符特殊文件,则为真 -d ...
- 1-9springboot之thymeleaf常用语法(html页面)
一.引用命名空间 <html xmlns:th="http://www.thymeleaf.org"> 在html中引入此命名空间,可避免编辑器出现html验证错误,虽 ...
- Java基础之循环语句、条件语句、switch case 语句
Java 循环结构 - for, while 及 do...while 顺序结构的程序语句只能被执行一次.如果您想要同样的操作执行多次,,就需要使用循环结构. Java中有三种主要的循环结构: whi ...
- thymeleaf常用语法
常用标签语法:①th:text<span th:text="${name}">1</span>注释:如果${name}有值则将替换掉1的值,若无则为1 ②t ...
- Thymeleaf常用语法:表达式语法之运算符
Thymeleaf表达式语法之常量分为字符串常量.数字常量.布尔值常量.空值常量:运算符分为算术运算符.关系运算符.条件运算符.无操作符. 开发环境:IntelliJ IDEA 2019.2.2Spr ...
- thymeleaf中的条件判断用法
一.简单的条件:“if”和“unless” th:if用法实例: <table> <tr> <th>NAME</th> <th>PRICE& ...
随机推荐
- docker安装redis 5.0.7并挂载外部配置和数据
环境 CentOS Linux release 7.7.1908 (Core) 拉取redis 5.0.7 镜像 docker pull redis:5.0.7 创建挂载目录 mkdir -p /ho ...
- SpringCloud断路器(Hystrix)
一.为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自身的原因,服务并不能保 ...
- IPIP.net识别客户端真实访问地址,具体到国家,省,市
这个IP库实测还是比较准确的,免费版的可以具体到国内城市,国外只能到国家名称,免费版的自己定期更新Ip数据库即可. 以下为C#调用代码 class Program { static void Main ...
- How to: Map a Persistent Class to a Database View Which Has No Key Field如何:映射持久化类到无主键数据库视图
With XAF, you can build new applications from scratch or maintain existing databases. The How to: Ge ...
- C# 程序一个cmd命令窗口执行多条dos命令
1,前几天的项目要用到程序执行dos命令去编译已生成的ice文件,后来去百度了好多都是只能执行一条命令 或者是分别执行几条命令,而我要的是一条dos命令在另一台命令的基础上执行.而不是分别执行. 后来 ...
- Dynamics 365中开发和注册插件介绍
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- English:Day-to-day 1015
device session stroll pants & trousers gist deep depth diameter D radius R merge ..
- color颜色大全
- Python中列表乘法需注意的问题/
前几天看到一个关于Python的面试题 lst = [1, 2, [3]] lst1 = lst * 2 # [1, 2, [3], 1, 2, [3]] lst1[2].append(4) # ...
- 【机器学习基础】交叉熵(cross entropy)损失函数是凸函数吗?
之所以会有这个问题,是因为在学习 logistic regression 时,<统计机器学习>一书说它的负对数似然函数是凸函数,而 logistic regression 的负对数似然函数 ...