首先要像如下所示改写html标签。
<html xmlns : th="http://www.thymeleaf. org">这样才可以在其他标签里面使用th:*这样的语法。 通过xmlns:th="http://www.thymeleaf.org"命名空间,引入Thymeleaf模板引擎,将静态页面转换为动态页面。需要进行动态处理的元素都使用“th:”为前缀。
引入 URL。 Thymeleaf对于URL的处理是通过语法@{…}来处理的。
<a th:href="@{http://www.ithelei.com/123 .png}">绝对路径</a> <a th:href="@ {/}">相对路径</a> <a th:href="@{css/bootstrap.min.css}">Content 路径,默认访问 static 下的 css 文件夹</a>表达式 有一些专门的表达式,用来从模板中的WebContext获取请求参数、请求、会话和应用程 序中的属性,这些操作和JSP的EL表达式非常相似。 $ {x}将返回存储在Thymeleaf上下文中的变量x或作为请求Request作用域中的属性。
$ {param.x}将返回一个名为x的请求参数(可能是多值的)。 $ {session.x}将返回一个名为x的会话HttpSession作用域中的属性。 $ {application.x}将返回一个名为x的全局ServletContext上下文作用域中的属性。 获取变量值用$符号,对于JavaBean使用“变量名.属性名”方式获取,这点和EL表达式 一样。另外,$表达式只能写在th标签内部,否则不会生效。字符串操作 很多时候可能只需对一大段文字中的某一处进行替换,可以通过字符串拼接操作完成。
<span th:text="'Welcome to java,'+ ${user .name} + '!' ">还有一种更简捷的方式
<span th:text="| Welcome to java,${user.name} !|">运算符 在表达式中可以使用各类算术运算符,例如+、-、*、/、%。
th:with="isEven= ($ {prodStat. count} % 2 == 0) "逻辑运算符>、<、<=、>=、==、!=都可以使用,唯一需要注意的是,使用<>时应用它的 HTML转义符。
th:if="${prodStat.count} >1 " th:text="'Execution mode is '+ ( (${execMode} == 'dev')? 'Development':'Production') "条件判断 Thymeleaf中使用th:if和th:unless属性进行条件判断,标签只有在th:if中的条件成立时才 显示,th:unless与th:if恰好相反,只有表达式中的条件不成立时,才会显示其内容。
<a th:href="main.html" th:if="${username != null}">Loginl</a><br> <a th:href="main.html" th:unless="$ {username ! = null}"!>Login2</a><br>Thymeleaf同样支持多路选择Switch结构,默认属性default可以用*表示。
<div th:switch="${role}"> <p th:case="'admin'">User is an administrator</p> <p th:case="'manager'">User is a manager</p> <p th:case="*">User is some other thing</p> </div>循环 Thymeleaf中循环变量集合使用th:each标签。 th:each属性用于迭代循环,迭代对象可以是java.util.List、java.util.Map或数组等,语法如下。
th :each=“obj,iterStat:$ {objList}”
下面是一个简单循环。
<tr th: each="book : $ {books}"> <td th:text="${book. title}">java</td> <td th: text="$ {boot: author}">博文</td> <td th: text="$ {boot. remark}" Spring Boot ! </td> </tr>iterStat称作状态变量,有以下这些属性。
index:当前迭代对象的index (从0开始计算)。count:当前迭代对象的index (从1开始计算)。size:被迭代对象的大小。current:当前迭代变量。even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)。first:布尔值,当前循环是否是第一个。last:布尔值,当前循环是否是最后一个。 <table border="1"> <tr> <th>用户名</th> <th> 邮箱 </th> <th>状态变量:index</th> <th>状态变量:count</th> <th>状态变量:size</th> <th>状态变量:current.username</th> <th>状态变量:even</th> <th>状态变量:odd</th> <th>状态变量:first</th> <th>状态变量:last</th> </tr> <tr th: each="user,userStat : ${list}"> <td th:text="${user.username}">admin</td> <td th:text="${user.email}">ithelei@sina.cn</td> <th th: text="${userStat.index}">状态变量:index</th> <th th: text="${userStat.count}">状态变量:count</th> <th th: text="${userStat.size}状态变量:size</th> <th th: text="${userStat.current.username}">状态变量:current</th> <th th: text="${userStat.even}">状态变量:even****</th> <th th: text="${userStat.odd}">状态变量:odd</th> <th th: text="${userStat.first}">状态变量:first</th> <th th: text="${userStat.last}">状态变量:last</th> </tr> </table>内置对象 Web项目中经常会传递一些数据,例如String类型的列表、日期对象和数值,这些在实 际应用开发中应用非常广泛,所以Thymeleaf还提供了很多内置对象,可以通过#直接访问。 内置对象一般都以s结尾,如dates、lists、numbers等。在使用内置对象时 需要在对象名前加#号。
在Thymeleaf中的内置对象有以下这些。
#dates:日期格式化内置对象,具体方法可以参照java.util.Date。 #calendars:类似于#dates,但是是 java.util.Calendar 类的方法。 #numbers:数字格式化。 #strings:字符串格式化,具体方法可以参照 java.lang.String,如 startsWith、contains 等。 #objects:参照 java.lang.Object〇 #bools:判断boolean类型的工具。 #arrays:数组操作的工具。 #lists:列表操作的工具,参照java.util丄ist。 #sets: Set 操作工具,参照 java.util.Set〇 #maps: Map 操作工具,参照 java.util.Map。 #aggregates:操作数组或集合的工具。 #messages:操作消息的工具。
示例代码如下
<span th:text="$ {#dates.format (curDate, 'yyyy-MM-dd HH :mm: ss') }"></span>使用内置对象dates的format函数即可对日期进行格式化,在format函数中,第一个参数 是日期对象,第二个参数为日期格式(规则与SimpleDateFormat—样)。
<span th:text="$ { #numbers.formatDecimal (money, 0, 2) }"></span>以上代码表示保留2位小数,整数位自动。
<span th: text="$ {#numbers.formatDecimal (money,3,2) }"> </span>以上代码表示保留2位小数,3位整数(不够的在前面加0)。
<span th:text="${#strings.length (str) }"</span>以上代码表示获取变量str的长度。
<span th:text="${#lists。size (datas) } "></span>以上代码表示使用#lists.size来获取名称为datas的集合的长度。