基本语法

表达式

${}变量表达式
  1. <p th:text="${name}">name</p> //th:text不能识别标签
  2. <p th:utext="${name}">name</p> //th:utext能识别传过来标签

*{}选择变量表达式
<div th:object="${res}"> res对象
    <p th:text="*{boss}">boss</p> boss对象中的属性
    <p th:text="*{chef}">chef</p>
</div>

@{}Url表达式
<a href="/welcome" th:href="@{/welcome}">welcome</a>

#{}消息表达式
会去messages.properties中读
something=好好学习
从配置文件获取值
<p th:utext="#{something}">message</p>

~{}片段表达式
定义片段
 <div th:fragment="片段名"></div>
使用片段
<div th:replace="~{模板名(文件名)::片段名}">
  eg:<div th:replace="~{index::footer}"></div>

符串拼接

<p th:text="${person.name}+'is'+${person.age}"></p>
也可以
<p th:text="|${person.name}+is+${person.age}|"></p>//会自动识别 不用加引号

条件判断

<p th:text="${msg}" th:if="${msg=='yes'}"></p>
除非(不等于)
传入的值不是no展示标签的内容
<p th:unless="${msg=='no'}">不等于no</p>

switch使用

<div th:switch="${number}">
    <p th:case="1">我是1</p>//如果number是1 展示标签的内容
    <p th:case="2">我是2</p>
    <p th:case="*">0</p> //如果number都不是以上的展示0
</div>

foreach循环

<tr th:each="data:${list}">//变量:集合
    <td th:text="${data}"></td>//获取变量的值
</tr>
获取状态变量:变量Stat例如  ${dataStat}
状态变量的属性   index 索引从0开始
               conut 计数 从1开始
               size  集合大小
               current当前对象
               first/last判断是否是第一个/最后一个 布尔值
               odd/even 判断count是否是奇数/偶数   布尔值

URL

发现:其实thymeleaf中th:后面的和html的属性大多是相同的(th:action th:href)有特殊的

${}来接收传来的值 @{}设置请求路径的

@{}来设置请求路径
<form th:action="@{/login}" method="post">
    username:<input type="text" name="username">
    password:<input type="password" name="password">
    <input type="submit">
</form>

绝对路径和相对路径的渲染

绝对路径

<a th:href="@{http://www.baidu.com}">跳转</a>http://www.baidu.com
eg:<a th:href=http://www.baidu.com>

<script type="text/javascript" th:src="@{//code.jquery.com/jquery-3.4.1.min.js}"/>
eg:   //--会自己判断是http还是https
<script type="text/javascript" src="//code.jquery.com/jquery-3.4.1.min.js"/>

相对路径

项目部署的地址localhost:8080/demo

<!--上下文相关的URL-->
<form th:action="@{/login}" method="post">localhost:8080/demo/login
eg:<form th:action="/demo/login" method="post">
<!--服务器相关URL-->
<a th:href="@{~/a.html}">连接</a>localhost:8080/a.html
eg:<a th:href="/a.html">连接</a>
<!--路径上设置参数-->
/addPreson?id=1
@{/addPreson(id=1)} 
/addPreson?id=1&name=aa
@{/addPreson(id=1,name=aa)}
/addPreson/1?name=aa
@{addPreson/{id}(id=1,name=aa)}

内置对象

temeleaf中的工具类
datas
calendars           
numbers
strings
objects
bools
arrays
lists
sets
Maps

日期

<p th:text="${#dates.format(date,'yyyy-MM-dd')}"></p> //date是传过来的数据
<p th:text="${#dates.createNow()}"></p>//获取当前的日期 没有格式化
<p th:text="${#dates.createToday()}"></p>//获取当前的日期没有时分秒 没有格式化

Strings

str传过来的内容
<p th:text="${#strings.isEmpty(str)}"></p> 判断是否为空
<p th:text="${#strings.length(str)}"></p>  字符串的长度
<p th:text="${#strings.equals('aaa',str)}"></p> 判断两个字符串是否相等

OGNL

 直接运算
 <p th:text="${1 * 2+ 3 - 4}"></p>

List list = new ArrayList();
list.add(11);
model.addAttribute("list",list);
通过传来的key直接获取
<p th:text="${list[0]}"></p>
可以获取java对象中的方法
<p th:text="${T(java.lang.Math).random()}"></p>

内联表达式

<p th:text="|加油${canshu}|"></p>
上述简化成下面的样子  任何可以通过th:text 都可以通过下面的方式获取
<p>加油[[${canshu}]]</p>
th:utext可以通过
<p>加油[(${canshu})]</p>

禁止内联

如果文本是[[aaa]]这个样子的
禁用内联 通过 thLinline="none“禁止内联
<p th:inline="none">加油[[${wuhan}]]</p> ---> [[${info}]]

JS和CSS中的使用

内联javaScript也可以在css中使用
注意只能在html中的js css中使用 外部的不可以
可以直接把变量在js中使用
<script type="text/javascript" th:inline="javascript">
    var info = [[${wuhan}]]
    console.log(info)
</script>