thymeleaf功能介绍

1.创建HTML

<html xmlns:th="http://www.thymeleaf.org">

2.获取变量值${…}

<p th:text="'Hello!, ' + ${name} + '!'">3333</p>

3.链接表达式: @{…} 用来配合link src href使用的语法,类似的标签有:th:href和th:src
4.循环
通过th:each
5.遍历

<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>

可遍历的对象:实现java.util.Iterable、java.util.Map(遍历时取java.util.Map.Entry)、array、任何对象都被当作只有对象自身一个元素的列表

6.对空值的处理
从后台传递过来的数据为空值,需要对操作对象进行空值判断,采用三目运算符进行判断.

<input type="email" id="salesemail" th:value="${sales!=null?sales.email:''}"  placeholder="Email" autocomplete="off" class="layui-input  layui-readonly" disabled="true"  >	

判断String字符串,添加引号

th:class="${flag=='forum.html'}?'active'"

判断boolean类型,注意不能当成字符串处理,不能添加引号th:style="${session.simpleFlag==false}?'':'color:red'"
判断数字,注意不能直接使用小于号,会被当成html的元素标签,导致编译出错

th:style="${pageNo}-3>0?'':'display: none'"

比较两个后台数据大小

th:style="${sum}-1>${pageNo}?'':'display: none'"

判断是否等于null

th:style="${session.dbUser==null}?'display:none;':'padding:0px 0px 0px 20px;'"
th:unless="${sub.publishDate}==null"   或者  th:unless="${sub.publishDate==null}" 

两种方法都行

发布了45 篇原创文章 · 获赞 9 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/zhanglinlove/article/details/90957024