码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot中Thymeleaf基本语法

时间:2019-08-21 23:10:22      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:地址   迭代   等价   stat   视频   默认   官网   三目运算   ase   

SpringBoot中Thymeleaf基本语法

本节咱们介绍Thymeleaf的基本语法


学习视频: http://www.itlaoqi.com/chapter/1688.html

源码地址: QQ群 814077650 , 群共享中自助下载

老齐的官网: itlaoqi.com (更多干货就在其中)


页面中显示Model中的变量

  • 使用${}获取Model中的属性值
  • 在标签中增加th:text显示数据
  • 或在标签体增加[[表达式]]显示数据
<td th:text="${emp.empno}"></td>
<td>[[${stat.index}]]</td>
两者等价

分支与判断

Thymeleaf使用下面三种方式实现分支与判断

  • th属性中使用三目运算符
th:text="${emp.comm }!= null? ${emp.comm} : 'N/A'"
  • th:if / th:unless 判断标签是否输出
<a href="#" th:if="${size>0}">
<a href="#" th:unless="${size>0}">
  • 多分支判断
    th:switch th:case
<td th:switch="${emp.dname}">
    <span th:case="RESEARCH">研发部</span>
    <span th:case="SALES">销售部</span>
    <span th:case="ACCOUNTING">会计部</span>
    <span th:case="*">其他部门</span>
    <!--在thymeleaf中只有th:if / th:unless 没有th:elseif -->
</td>

迭代遍历

  • 使用th:each属性迭代。
  • 格式为: 迭代对象,状态对象 : 集合
<tr th:each="emp,stat:${emps}" >
    <td>[[${stat.index+1}]]</td>
    <td>[[${#dates.format(emp.hiredate , 'yyyy年MM月dd日')}]]</td>
</tr>

stat.index代表索引值,默认从0开始

时间、数字进行格式化

thymeleaf提供了一些列内置对象

  • dates - 日期格式化对象

  • numbers - 数字格式化对象

  • strings - 字符串格式化对对象

  • lists、#sets、#maps 集合操作对象

<td th:text="${emp.comm!=null}?${#numbers.formatCurrency(emp.comm)}:'N/A'"></td>
<td>[[${#dates.format(emp.hiredate , 'yyyy年MM月dd日')}]]</td>
<td>[[${#strings.toLowerCase(emp.ename)}]]</td>

获取请求参数

  • ${param.xxx} 用于获取请求参数
  • 例如${param.keyword}
  • 相当于request.getParameter(“keyword”)
  • 除此以外,还有${request}、${session}、${application}但不推荐使用。

SpringBoot中Thymeleaf基本语法

标签:地址   迭代   等价   stat   视频   默认   官网   三目运算   ase   

原文地址:https://www.cnblogs.com/itlaoqi/p/11391787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!