说明:

本项目是在

https://blog.csdn.net/weixin_59334478/article/details/126749179?spm=1001.2014.3001.5501https://blog.csdn.net/weixin_59334478/article/details/126749179?spm=1001.2014.3001.5501

在springboot中使用thymeleaf循环

springboot文章23

修改而来

pom.xml文件,核心配置文件等文件,与文章23保持一致,本文只展示最新添加的文件,以减小文章冗余,更容易看出循环的使用方法。

项目:

一、if-unless
语法:
th:if=”boolean 条件 , 条件为 true 显示体内容
th:unless th:if 的一个相反操作
1.index.html添加连接语句

 2.ThymeleafController类添加方法

3.创建ifunless的html文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>if-unless</title>
</head>
<body>
<div style="margin:auto;">
    <h3>if使用:判断条件为true时,显示标签体内容</h3>
    <p th:if="${sex=='m'}">性别是男</p>
    <p th:if="${isLogin}">已经登录系统</p>

    <p th:if="${age>20}">年龄大于20</p>
    <!--空字符串是真-->
    <p th:if="${name}">name是“”</p>
    <!--null是false-->
    <p th:if="${isOld}">isOld是null</p>
</div>
<br/>
<br/>
<div style="margin: auto;">
    <h3>unless的使用:判断条件为false时,显示标签体内容</h3>
    <p th:unless="${sex=='m'}">性别是男</p>
    <p th:unless="${isLogin}">登录系统</p>
    <p th:unless="${isOld}">isOld是null</p>
</div>

</body>
</html>

4.if-unless的测试

 点击判断语句If和unless

二、switch,case判断语句

语法:类似 java 中的 switch case
一旦某个 case 判断值为 true,剩余的 case 则都当做 false,“*”表示默认的 case,前面的 case 都不匹配时候,执行默认的 case。
1.index.html添加连接语句

 2.ThymeleafController类添加方法

3.创建switch的html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>if-unless</title>
</head>
<body>
<div style="margin:auto;">
    <h3>switch的使用</h3>
    <div th:switch="${sex}">
        <p th:case="w">性别是女</p>
        <p th:case="m">性别是男</p>
        <p th:case="*">性别未知</p>
    </div>
</div>

</body>
</html>
4.switch的测试

当sex的值为w时

当sex的值既不是m又不是w时

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐