JAVA----Thymeleaf 常用语法

变量

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值