Thymeleaf使用总结
标签(空格分隔): Thymeleaf
引入命名空间
<html xmlns:th="http://www.thymeleaf.org">
引入可避免html验证错误,加不加对ThymeLeaf功能无影响
输出内容
<p th:text="#{home.welcome}">Welcome to our grocery store!</p>
- th:text 用于输出内容
- #{home.welcome} 引入对象属性
- th:utext 显示“unescaped(未转义)”的html内容
访问对象
${param.x}
返回名为x的request参数,可能有多个值${session.x}
返回名为x的session参数${application.x}
返回名为x的servlet context参数
基本语法
- 访问数据
#{home.welcome}
- 访问变量
${today}
- 访问基本变量
变量名 | 对象名 |
---|---|
#ctx | the context object |
#vars | the context variables |
#locale | the context locale |
#request | (only in Web Contexts) the HttpServletRequest object |
#response | (only in Web Contexts) the HttpServletResponse object |
#session | (only in Web Contexts) the HttpSession object |
#servletContext | (only in Web Contexts) the ServletContext object |
其它公共对象
4. 日期输出
<span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span>
- 星号语法
*
取上一层的变量作为父节点
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
- 输出URL
<a href="product/list.html" th:href="@{/product/list}">Product List</a>
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
- 使用代码段
<div th:insert="~{commons :: main}">...</div>
- 直接输出内容
<span th:text="'working web application'"> -- 输出字符
<span th:text="2013 + 2"> -- 输出数据表达式
<div th:if="${user.isAdmin()} == false"> --输出布尔表达式
<span th:text="'Welcome to our application, ' + ${user.name} + '!'"> -- 带变量的
- 条件表达式
<tr th:class="${row.even}? 'even' : 'odd'">... </tr>
<tr th:class="${row.even}? 'alt'">...省略 false 结果的表达方式</tr>
<div th:object="${session.user}">
...省略 true 结果的表达方式
<p>Age: <span th:text="*{age}?: '(no age specified)'">27</span>.</p>
</div>
<span th:text="${user.name} ?: _">--不做任何处理时用下划线 _ 表示</span>
设置Attribute值
- 设置任何Attribute值
--设置单个
<input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
--一次设置多个
<img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />
- 设置一些内置Attribute
<li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li>
<form action="subscribe.html" th:action="@{/subscribe}">
<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>
<img src="../../images/gtvglogo.png" th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" />
##循环输出
- 基本循环
<tr th:each="prod : ${prods}">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
- 循环状态变量
<table>
<tr>
<th>产品名称</th>
<th>产品价格</th>
<th>有现货</th>
</tr>
<tr th:each="prod,iterStat:${prods}" th:class="${iterStat.odd}?'odd'">
<td th:text="${prod.name}">土豆</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}?#{true}:#{false}">yes</td>
</tr>
</table>
iterStat为状态变量,它有以下属性
index,获取当前迭代从0开始的下标
count,获取当前迭代从1开始的下标
size,获取当前迭代元素总量
curren,获取迭代变量中的迭代值
even/odd,当前迭代值是奇数还是偶数
first,当前迭代值是否是第一个元素
last,当前迭代值是否是最后一个元素
条件判断
- if和unless
<a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">view</a>
<a href="comments.html" th:href="@{/product/comments(prodId=${prod.id})}" th:if="${not #lists.isEmpty(prod.comments)}">view</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> --默认的 case 相当于default
</div>
模版引用
- 定义和引用代码块
--定义fragment
<div th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</div>
--引用fragment
<div th:insert="~{footer :: copy}"></div>
--引用id代码块
<div id="copy-section">
© 2011 The Good Thymes Virtual Grocery
</div>
<div th:insert="~{footer :: #copy-section}"></div>
th:insert
th:replace
th:include
区别
th:insert
:插入代码块
th:replace
:替换代码块会替换容器标签
th:include
:和insert相似但是只插入fragment标注的内容
- 带参数的代码段
<div th:fragment="frag (onevar,twovar)">
<p th:text="${onevar} + ' - ' + ${twovar}">...</p>
</div>
<div th:replace="::frag (${value1},${value2})">...</div>
<div th:replace="::frag (onevar=${value1},twovar=${value2})">...</div>
局部变量
<div th:with="firstPer=${persons[0]}">
<p>
The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.
</p>
</div>
注释
<!--..