变量
Thymeleaf 的主要作用是把 model 中的数据渲染到html 中
model 中的变量
${...}
语法和 el 表达式差不多,但它是 ognl 表达式
自定义变量
<div th:object="${user}">
<h2 th:text="*{name}"></h2>
<h2 th:text="*{age}"></h2>
<!--/*@thymesVar id="friend" type="com.xp.entity.Friend"*/-->
<h2 th:text="*{friend.name}"></h2>
</div>
script
<script type="text/javascript" th:inline="javascript">
//协议名称
var scheme = [[${#request.getScheme()}]];
//服务器名称
var serverName = [[${#request.getServerName()}]];
//服务器端口号
var serverPort = [[${#request.getServerPort()}]];
//上下文根
var contextPath = [[${#request.getContextPath()}]];
//获取请求参数
var queryString = [[${#request.queryString}]];
//获取请求路径(不带参数)
var requestURL = [[${#request.requestURL}]];
alert(queryString);
</script>
th
th:id 替换id <input th:id="'xxx' + ${collect.id}"/>
th:text 文本替换 <p th:text="${collect.description}">description</p>
th:utext 支持html的文本替换 <p th:utext="${htmlcontent}">conten</p>
th:object 替换对象 <div th:object="${session.user}">
th:value 属性赋值 <input th:value="${user.name}" />
th:with 变量赋值运算 <div th:with=“isEven=${prodStat.count}%2==0”>< /div>
th:style 设置样式 th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"
th:onclick 点击事件 th:οnclick="'getCollect()'"
th:each 属性赋值 tr th:each=“user,userStat:${users}”>
th:if 判断条件 <a th:if="${userId == collect.userId}">
<td th:class="${server.disk.usage gt 80} ? 'text-danger'">[[${server.disk.usage}]]%</td>
<div class="layui-progress layui-progress-big" lay-showPercent="yes">
<div
th:class="${disk.usage gt 80} ? 'layui-progress-bar layui-bg-red': 'layui-progress-bar layui-bg-blue'"
th:lay-percent="${disk.usage} + '%'" th:text="${disk.usage} + '%'"
>%0</div>
</div>
逻辑判断
<div th:if="${user.age} >= 18">
<h1>成年人</h1>
</div>
分控制
<div th:switch="${user.role}">
<p th:case="'admin'">用户是管理员</p>
<p th:case="'manager'">用户是经理</p>
<p th:case="*">用户是别的玩意</p>
</div>
循环
<div th:each="list:${lists}">
<h1 th:text="${list}"></h1>
</div>