文件参考:https://www.jianshu.com/p/f79a98173677
1.基本表达式
1.1:变量表达式${}
<span th:text="${information}"></span>
1.2:选择变量表达式*{}:一般配合@ModelAttribute使用,注解用法
//java的controller
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {...}
//thymeleaf的html页面
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...
<input type="text" value="" th:field="*{username}"></input>
<input type="text" value="" th:field="*{user[0].username}"></input>
</form>
1.3:信息表达式#{}
<p th: text=" #{home. welcome}" >This text will not be show! </p>
home.welcome=this messages is from home.properties!
1.4:链接URL表达式@{}
<link th:src="@{/resources/css/bootstrap.min.css}" />
<li><a th:href="@{/forIndex}">首页</a></li>
1.5:工具类表达式 ${#map} (详情参考)
<div th:if="${#maps.size(stu)!=0}">
...do something...
</div>
2.常用属性
th:action 表单提交的地址 <form action="subscribe.html" th:action="@{/subscribe}">
th:each 属性赋值 <tr th:each="user,userStat:${users}">
th:field 常用于表单字段绑定 <input type="text" value="" th:field="*{username}"></input>
th:href 链接地址 <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> />
th:id 替换id <input th:id="'xxx' + ${collect.id}"/>
th:if 判断条件 <a th:if="${userId == collect.userId}" >
th:include 布局标签,替换内容到引入的文件 <head th:include="layout :: htmlhead" th:with="title='xx'"></head> />
th:fragment 布局标签,定义代码片段,其它引用 <div th:fragment="alert">
th:object 替换对象 <div th:object="${session.user}">
th:src 图片类地址引入 ![](@{/img/logo.png})
th:replace 布局标签,替换整个标签到引入的文件 <div th:replace="fragments/header :: title"></div>
th:text 文本替换 <p th:text="${collect.description}">description</p>
th:value 属性赋值 <input th:value="${user.name}" />
th:inline js脚本中可以使用thymeleaf变量 <script type="text/javascript" th:inline="javascript">
th:remove 删除某个属性 <tr th:remove="all">
th:style 设置样式 th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"
th:onclick 点击事件 th:onclick="'getCollect()'"
3.标准表达式
3.1:普通字面
<span th:text="'working web application'">template file</span> //working web application
<div th:if="${user.isAdmin()} == false"> //true
<div th:if="${variable.something} == null"> //null
3.2:文字操作
<span th:text="'Welcome to our application, ' + ${user.name} + '!'"> //Welcome to our application,liming
<span th:text="|Welcome to our application, ${user.name}!|">//Welcome to our application,liming
3.3:算数运算
3.3.1:二元运算 +,-,*,/,%
<span th:text="1+1">1+1</span>
3.3.2:布尔运算 and ,or, !, not
<span th:if="${!#lists.isEmpty(list)} and ${#lists.isEmpty(list)}" >and</span>
<span th:if="${!#lists.isEmpty(list)} or ${#lists.isEmpty(list)}" >or</span>
<span th:if="${!#lists.isEmpty(list)}">not</span>
3.3.3:比较运算 (>,<,<=,>=,==,!=(gt,lt,le,ge,eq,ne))
<ol>
<li>>(gt):<span th:text="1+1" th:if="${#lists.size(list)} > 1">大于></span>else</li>
<li>小于lt:<span th:if="${#lists.size(list)} lt 1">小于</span>else</li>
<li>>=(ge):<span th:if="${#lists.size(list)} >= 1">大于等于>=</span>else</li>
<li>小于等于(le):<span th:if="${#lists.size(list)} le 1">小于等于</span>else</li>
<li>!(not):<span th:if="${!#lists.isEmpty(list)}">!(not)</span>else</li>
<li>==(eq):<span th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')">等于==</span></li>
<li>!=(ne/neq):size:<span th:text="${#lists.size(list)}" th:if="${#lists.size(list)} != 1"></span></li>
</ol>
3.3.4:条件运算 if ?then , if? then:else , if ? :default
<span th:class="${title1} ? 'green'">样例</span>
<span th:class="${title} ? 'green' :' red'">样例一</span>
<span th:text="*{age}?: '(no age specified)'">20</span>