Thymeleaf 整理

1、标准变量表达式:
  thymeleaf中的变量表达式使用${变量名}的方式获取其中的数据
  th:text="" 是thymeleaf的一个属性,用于文本的显示
  如:<span th:text="${user.name}">X</span>
2、选择变量表达式
  也叫星号表达式,使用th:object属性来绑定对象
  <div th:object="${user}"> ---------------绑定对象
  <p>nick:<span th:text="*{nick}">张</span></p>----------nick为user的属性*代表user
  </div>
3、url表达式
  语法:@{..}
  url表达式可用于<script src="..."> 、<link href=".."> 、<a href="..">等
  <script th:src="@{/js/jquery-2.4.min.js}"></script> 前面有/,默认加上项目跟

4、thymeleaf的表达式
  th:action 配合url表达式使用
  th:each 循环展示数据(list map array)

  <p th:each="user:${userlist}">
    <span th:text="{userstat.count}">张</span>---userstat默认实体+stat 表示循环 体信息
    <span th:text="{user.nick}">张</span>
  </p>
  <p th:each="user,iterStat:${userlist}"> -----------自己声明的iterStat循环体
    <span th:text="{iterStat.count}">张</span>
    <span th:text="{user.nick}">张</span>
  </p>

  循环体信息 如下:index当前迭代对象的index
    count当前迭代对象的个数
    size 被迭代对象的大小
    current 当前迭代变量
    first 布尔值 当前循环是否是第一个

    ......

  <p th:each="user:${userMap}">
    <span th:text="{user.key}">张</span>
    <span th:text="{user.value.nick}">张</span>
  </p>

  th:href 定义超链接
  th:id 类似html标签中的id属性
  th:name 类似html标签中的name属性
  th:if 条件判断
  th:unless 与th:if 相反操作
  th:switch/th:case 判断语句
  <div th:switch="${sex}">
    <p th:case="1">性别:男</p>
    <p th:case="2">性别:男</p>
    <p th:case="*">性别:未知</p>
  </div>
  一旦某个case判断值为true,剩余的case则都当做false;
  "*" 表示默认的的case,前面的case都不匹配的时候,执行默认的case

  th:object 用于数据对象绑定 ,通常用于选择变量表达式
  th:src 用于外部资源引入 通常与@{}表达式结合使用
    <script th:src="@{/js/jquery-2.4.min.js}"></script>
  th:text 文本显示
  th:value 类似html标签中的value的属性 赋值
  th:attr 为属性赋值,可以用动态属性赋值
  th:attr="value=${user.name}"
  th:onclick 点击事件 th:onclick="'getCollect()'" 注意""里面的单引号
  th:style 也要注意""号里面的单引号
  th:method 设置请求方法
  th:inline 内联文本 内联脚本。有三个取值类型 text javascript 和none
  该属性使用内联表达式[[..]]展示变量数据,
  比如:内联文本
  <span th:inline="text">Hello,[[${user.nick}]]</span>
  等同于
  <span>Hello,<span th:text="${user.nick}"></span></span>
  再如:内联脚本
  <script th:inline="javascript" type="text/javascript">
    var user=[[${user.nick}]]
    alert(user)
  </script>
  当值为none时 可省略
5、thymeleaf字面量
  文本字面量:用单引号''包围的字符串为文本字面量
  数字字面量:可以直接写数字 也可以进行+-*/操作
  boolean字面量: true和false
  <p th:if="${isFlag==true}">
    执行操作
  </p>
  null字面量:null
  <p th:if="${isFlag!=null}">
    执行操作
  </p>
6、字符串拼接
  <span th:text="'当前是第'+${num}+'页,共'+${total}+'页'"></span>
  <span th:text="|当前是第${num}页,共${total}页|"></span>
7、三元运算判断
  <span th:text="${sex eq '1'}?'男':'女'">未知</span>
8、thymeleaf 内置对象
  #request 3.0
  #httpServletRequest 2.0
    <span th:text="${#request.getContextPath()}">
    <span th:text="${#request.getAttribute('name')}">
  #session 3.0
  #httpSession 2.0
    <span th:text="${#session.getAttribute('name')}">
    <span th:text="${#session.id}">
    <span th:text="${#session.lastAccessedTime}">
9、thymeleaf 功能性内置对象
  比如#dates、#arrays #strings等等 更多具体可以查官方文档
  <span th:text="${#dates.format(curDate,'yyyy-MM-dd HH:mm:ss')}"></span>

https://www.thymeleaf.org/

猜你喜欢

转载自www.cnblogs.com/GtShare/p/9330993.html