引入公共片段的三种方法
<footer th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</footer>
<!-- 方法1 -->
<div th:insert="footer :: copy"></div>
<!-- 效果 -->
<div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
</div>
<!-- 方法2 -->
<div th:replace="footer :: copy"></div>
<!-- 效果 -->
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
<!-- 方法3 -->
<div th:include="footer :: copy"></div>
<!-- 效果 -->
<div>
© 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>");