thymeleaf语法备忘

引入公共片段的三种方法

<footer th:fragment="copy">
&copy; 2011 The Good Thymes Virtual Grocery
</footer>

<!-- 方法1 -->
<div th:insert="footer :: copy"></div>
<!-- 效果 -->
<div>
    <footer>
    &copy; 2011 The Good Thymes Virtual Grocery
    </footer>
</div>

<!-- 方法2 -->
<div th:replace="footer :: copy"></div>
<!-- 效果 -->
<footer>
&copy; 2011 The Good Thymes Virtual Grocery
</footer>

<!-- 方法3 -->
<div th:include="footer :: copy"></div>
<!-- 效果 -->
<div>
&copy; 2011 The Good Thymes Virtual Grocery
</div>

设置display样式

th:style="'display:' + @{(${value} ? 'none' : 'inline-block')} + ''"

th:text中插变量

<span th:text="'Welcome to our application, ' + ${user.name} + '!'">

运算符

±*/% -(负) and or ! not
比较可以用 gt lt ge le eq ne代替符号
条件运算:(if) ? (then):(else)
(val) ?: (defaultVal)
th:unless是表达式不成立显示内容

<a th:href="@{/login}" th:unless=${session.user != null}>Login</a>

switch

<div th:switch="${user.role}">
  <p th:case="'admin'">User is an administrator</p>
  <p th:case="#{roles.manager}">User is a manager</p>
  <p th:case="*">User is some other thing</p>
</div>

循环

<tr th:each="emp : ${empList}">
    <td th:text="${emp.id}">1</td>
    <td th:text="${emp.name}"></td>
    <td th:text="${emp.age}">18</td>
</tr>

常用标签

在这里插入图片描述

前端展示一个对象的属性

第一种方式:
<div>
    <p th:text="${member.uid}" />
    <p th:text="${member.name}" />
</div>
第二种方式:
<div th:object="${member}">
    <p th:text="*{uid}"/>
    <p th:text="*{name}"/>
</div>
两种方式的区别是,$可以访问完整信息,*只访问属性。如果访问的只是普通内容则无区别。

th:utext中可以展示html风格的字符串

html中
<p th:utext="'用户名称:' + ${name}"/>
controller中
model.addAttribute("name","<span style='color:red'>Jerry</span>");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值