1. 概述

这里介绍thymeleaf的编程语法,本节主要包括如下内容

  1. 迭代语法:th:each; iteration status
  2. 条件语法:th:if; th:unless
  3. switch语法:th:switch; th:case; *

下文演示以上语法的用法。

2. 演示以上语法的用法

2.1. 公共类

User

public class User {
private String name;
private boolean isAdmin;
private String other;
private int age;
public User(String name, boolean isAdmin, String other, int age) {
super();
this.name = name;
this.isAdmin = isAdmin;
this.other = other;
this.age = age;
}
// set/get略
}

ProgrammingCtl : control类

@Controller
@RequestMapping("/programming")
public class ProgrammingCtl { @RequestMapping("programming")
public String iteration(ModelMap modeMap) {
// Iteration
List<User> userList = new ArrayList<User>();
userList.add(new User("son_1", true, "other_1", 11));
userList.add(new User("son_2", false, "other_2", 22));
userList.add(new User("son_3", true, "other_3", 33));
userList.add(new User("son_4", false, "other_4", 44));
modeMap.put("userList", userList); // ifelse
User userIf = new User("admin", true, "other_if", 11);
modeMap.put("user", userIf); return "programming/programming";
}
}

本请求转到页面programming.html,

2.2. 迭代语法:th:each; iteration status

常用th:each用法:

<table border="2">
<thead>
<tr>
<th>name</th>
<th>age</th>
<th>isAdmin</th>
</tr>
</thead>
<tbody>
<!-- 常用的迭代 th:each 用法 -->
<tr th:each="user : ${userList}">
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td th:text="${user.isAdmin}"></td>
</tr>
</tbody>
</table>

运行结果如下: 

迭代的对象 
本例子中迭代的对象是java.util.List,除了List,还可以对以下对象进行迭代

  • java.util.Iterable
  • java.util.Enumeration
  • java.util.Iterator
  • java.util.Map,此时迭代返回的对象是java.util.Map.Entry
  • 数组

获取迭代的中间的状态,定义在iterStat中

在迭代过程中,可以获取迭代的中间状态,详细如下:

  • index :当前节点的索引,从0开始
  • size : 迭代节点总数
  • even/odd:当前是偶数/奇数行,boolean值
  • first/last:当前是每天/最后一个元素
<t
able border="2">
<thead>
<tr>
<th>迭代索引</th>
<th>元素所处的位置索引</th>
<th>奇偶行</th>
<th>name</th>
<th>age</th>
<th>isAdmin</th>
</tr>
</thead>
<tbody>
<!-- 获取迭代的中间的状态,定义在iterStat中-->
<tr th:each="user,iterStat : ${userList}">
<!-- index: 当前迭代的索引 -->
<td th:text="${iterStat.index }"></td>
<!-- first: 当前元素是第一个元素; last: 当前元素是最后个元素 -->
<td th:text="${iterStat.first } ? '这是第一个元素':(${iterStat.last} ? '这是最后一个元素':'')" ></td>
<!-- -->
<td th:text="${iterStat.odd} ? 'odd' : 'even'" ></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td th:text="${user.isAdmin}"></td> </tr>
</tbody>
</table>

运行结果如下: 

2.3. 条件语法:th:if; th:unless

演示如下功能

  • th:if:如果值是true,则打印整个节点
  • th:unless: 和th:if是相反功能,如果值为false,则打印整个节点
    <!-- th:if:如果值是true,则打印<span>整个节点  -->
<span th:if="${user.isAdmin}" th:text="${user.name} + '是管理员'"> </span><br />
<!-- th:unless: 和th:if是相反功能,如果值为false,则打印<span>整个节点 -->
<span th:unless="not ${user.isAdmin}" th:text="${user.name} + '是管理员'"> </span><br />

输出:

<span>admin是管理员</span><br />
<span>admin是管理员</span><br />

th:if条件判断 
除了判断boolean值外,thymeleaf还认为如下表达式为true:

  • 值非空
  • 值是character,但是非0
  • 值是非0数字
  • 值是字符串,但是不是 “false”, “off” or “no”
  • 值不是boolean值,数字,character 或 字符串

2.4. switch语法:th:switch; th:case; *

演示如下功能

  • th:switch / th:case
  • th:case=”*” : 类似switch中的default
<!-- th:switch / th:case -->
<div th:switch="${user.name}">
<p th:case="'admin'">User is an administrator</p>
<!-- *: case的默认的选项 -->
<p th:case="*">User is some other thing</p>
</div>

输出:

<div>
<p>User is an administrator</p>
</div>

4.1算术操作符

+, -, *, /, %
e.g.

  1. <div>
  2. <ol>
  3. <li>+:1+1=<span th:text="1+1">1+1</span>.</li>
  4. <li>-: 2-1=<span th:text="2-1">2-1</span>.</li>
  5. <li>*:2*3=<span th:text="2*3">2*3</span>.</li>
  6. <li>/: 9/4=<span th:text="9/4">9/4</span>.</li>
  7. <li>%:9%4=<span th:text="9%4">9%4</span>.</li>
  8. </ol>
  9. </div>

4.2布尔运算

e.g.

  1. <div>
  2. <ol>
  3. <li th:inline="text">and:<span  th:if="${!#lists.isEmpty(list)} and ${#lists.isEmpty(list)}" th:text="${!#lists.isEmpty(list)} and ${#lists.isEmpty(list)}">and</span>[[${!#lists.isEmpty(list)} and ${#lists.isEmpty(list)}]]</li>
  4. <li>or:<span  th:if="${!#lists.isEmpty(list)} or ${#lists.isEmpty(list)}" th:text="${!#lists.isEmpty(list)} or ${#lists.isEmpty(list)}">or</span></li>
  5. <li>!(not):<span  th:if="${!#lists.isEmpty(list)}" th:text="${!#lists.isEmpty(list)}">not</span></li>
  6. </ol>
  7. </div>

4.3不等和相等运算符

e.g.

    1. <div>
    2. <ol>
    3. <li>比较表达式:
    4. <ol>
    5. <li>>(gt):<span th:text="1+1" th:if="${#lists.size(list)} > 1">大于></span>else</li>
    6. <li>小于lt:<span th:if="${#lists.size(list)} lt 1">小于</span>else</li>
    7. <li>>=(ge):<span  th:if="${#lists.size(list)} >= 1">大于等于>=</span>else</li>
    8. <li>小于等于(le):<span  th:if="${#lists.size(list)} le 1">小于等于</span>else</li>
    9. <li>!(not):<span  th:if="${!#lists.isEmpty(list)}">!(not)</span>else</li>
    10. </ol>
    11. </li>
    12. <li>相等和不等表达式:
    13. <ol>
    14. <li>==(eq):<span th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')">等于==</span></li>
    15. <li>!=(ne/neq):size:<span th:text="${#lists.size(list)}" th:if="${#lists.size(list)} != 1"></span></li>
    16. </ol>
    17. </li>
    18. </ol>
    19. </div>

3. 代码

代码详细见Github

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hry2015/article/details/73253080

Thymeleaf系列五 迭代,if,switch语法的更多相关文章

  1. Javascript数组系列五之增删改和强大的 splice()

    今天是我们介绍数组系列文章的第五篇,也是我们数组系列的最后一篇文章,只是数据系列的结束,所以大家不用担心,我们会持续的更新干货文章. 生命不息,更新不止! 今天我们就不那么多废话了,直接干货开始. 我 ...

  2. JVM系列五:JVM监测&工具

    JVM系列五:JVM监测&工具[整理中]  http://www.cnblogs.com/redcreen/archive/2011/05/09/2040977.html 前几篇篇文章介绍了介 ...

  3. SQL Server 2008空间数据应用系列五:数据表中使用空间数据类型

    原文:SQL Server 2008空间数据应用系列五:数据表中使用空间数据类型 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2008 R2调测 ...

  4. [时序图笔记] 步步为营UML建模系列五、时序图(Squence diagram)【转】

    概述 顺序图是一种详细表示对象之间以及对象与参与者实例之间交互的图,它由一组协作的对象(或参与者实例)以及它们之间可发送的消息组成,它强调消息之间的顺序. 顺序图是一种详细表示对象之间以及对象与系统外 ...

  5. Java 8系列之Stream的基本语法详解

    本文转至:https://blog.csdn.net/io_field/article/details/54971761 Stream系列: Java 8系列之Stream的基本语法详解 Java 8 ...

  6. 五. Python基础(5)--语法

    五. Python基础(5)--语法 1 ● break结束的是它所在的循环体, continue是让它所在的循环体继续循环 # 打印: 1 10 2 10 3 10 4 10 5 10 6 10 7 ...

  7. Hexo系列(五) 撰写文章

    在利用 Hexo 框架搭建一个属于我们自己的博客网站后,下面我们就来谈谈怎样在网站上书写我们的第一篇博客吧 一.创建文章 在站点文件夹中打开 git bash,输入如下命令创建文章,其中 title ...

  8. emmet 系列(1)基础语法

    emmet 系列(1)基础语法 emmet 是一个能显著提升开发html和css开发效率的web开发者工具 emmet基本上目前已知的编辑器都有相应的插件,各个编辑器的emmet插件的下载地址:点我下 ...

  9. CSS 魔法系列:纯 CSS 绘制各种图形《系列五》

    我们的网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多比人想象不到的效果.特别是随着 CSS3 的广泛使用,更多新奇的 CSS 作品涌现出来. ...

随机推荐

  1. git add 的一点说明

    git add --cached 这里 --cached是什么意思呢?要解释清楚这个问题,我们必须先了解一个文件在git中的状态. [commit]----[stage]-----[checkout] ...

  2. kali linux 2018.2 mysql密码修改后无效,外部无法连接问题。

    kali linux 2018.2 mysql密码修改后无效,外部无法连接问题 Kali Linux 2018.2 默认MySQL数据库是mariadb,可能和MySQL有些细微的变化,只需要做如下处 ...

  3. 为了更好更方便地活着——爱上private

    刚开始接触OOP的时候,打心底里我不喜欢private与protected.我声明一个public然后不直接用它,不就跟private一样吗?在某些场合下,我还能偷偷地用一下public变量,这不是更 ...

  4. linux 命令 --while

    shell while 语法: while [条件判断式] do 程序 done 对 while 循环来讲,只要条件判断式成立,循环就会一直进行,直到条件判断式不成立,循环才会停止. 例如: 循环从1 ...

  5. 『转』Dr.Web Security Space 8 – 免费3个月

    简短的测试五个问题,任意回答问题,都将获得Dr.Web Security Suite 3个月免费许可证以及大蜘蛛企业安全套件2个月来保护整个公司!活动地址:https://www.drweb.com/ ...

  6. np.ones(N)/N的作用

    在python中导入numpy包 N=5 weights = np.ones(N)/N       //这里就相当于创建了一个数组,且为5个1/5的数组 print "weights&quo ...

  7. 日志异常:java.lang.NoClassDefFoundError: Could not initialize class org.slf4j.impl.StaticLoggerBinder

    今天启动开发的项目,碰到了一个日志上的bug:java.lang.NoClassDefFoundError: Could not initialize class org.slf4j.impl.Sta ...

  8. 浅谈c#垃圾回收机制(GC)

    写了一个window服务,循环更新sqlite记录,内存一点点稳步增长.三天后,内存溢出.于是,我从自己的代码入手,查找到底哪儿占用内存释放不掉,最终明确是调用servicestack.ormlite ...

  9. NTP时间服务器的配置

      1.NTP简介NTP(Network Time Protocol,网络时间协议)是用来使网络中的计算机,时间同步的一种协议.NTP服务器利用NTP协议来提供时间同步服务. 2 .环境准备主机名   ...

  10. ThinkPHP think-migration 简洁使用教程

    ThinkPHP think-migration 简洁使用教程 migration:一种数据库的版本控制,让团队在修改数据库结构的同时,保持彼此的进度一致.帮你更简单的管理数据库.基于原生 think ...