thymeleaf学习笔记

thymeleaf学习笔记

简单表达式

  • ${…} 变量表达式; 变量值的替换,可以简单理解为后端注入到前端的变量值
<div>
    <p>Name: <span th:text="${session.user.firstname}">Sebastian</span></p>
</div>
  • *{…} 选择变量表达式;对比变量表达式,选择变量表达式优先选择指定的变量,若没有指定,则和变量表达式完全一致
<div th:object="${session.user}">
    <p>Name: <span th:text="*{firstname}">Sebastian</span></p>
</div>
  • #{…} 消息表达式;从属性文件中获取的值替换html文件中的占位符号
<p th:utext="#{home.welcome}">Welcome to our grocery store ! </p>
  • @{…} 链接url表达式;
<a th:href="@{'/details/'+${user.login}(orderId=${o.id})}">view</a>
<a th:href="@{~/tmp/logfile}">logfile</a>

文字类型

  • 字符型: ‘one text’ , ‘Another one!’
  • 数值型: 0 , 34 , 3.0 , 12.3
  • Boolean型: true , false
  • 空值: null
  • 文本字符串: one , sometext , main

字符串操作

  • 字符串连接: +
<span th:text="'The name of the user is ' + ${user.name}">
  • 文字替换: |The name is ${name}|
<span th:text="|The name of the user is  ${user.name}|">
  • 内联表达式:[[…]]

数值型操作:

  • 运算符: + , - , * , / , %
  • 负号: -
  • Boolean操作:
    • 运算符: and , or
    • 非运算符: ! , not
  • 比较相等算法:
    • 比较: > , < , >= , <= ( gt , lt , ge , le )
    • 相等算法: == , != ( eq , ne )

条件语句:

  • If-then: (if) ? (then)
<body>
    <div th:if="${age} > 20">
        大于 20
    </div>
    <div th:unless="${age} > 20">
        小于 20
    </div>
</body>
  • If-then-else: (if) ? (then) : (else)
<tr th:class="${row.even} ? 'even' : 'odd'">error</tr>
  • switch
<div th:switch="${role}">
    <p th:case="user">User</p>
    <p th:case="${admin}">Admin</p>
    <p th:case="*">Unknown</p>
</div>

循环

  • each
<ul>
    <li th:each="animal : ${animals}" th:text="${animal}">小动物 - 此处文本会被覆盖</li>
</ul>

模版语法

  • 定义模版片段
<div th:fragment="copy">
      &copy; 2011 The Good Thymes Virtual Grocery
    </div>
  • 引入模版
<div th:insert="~{footer :: copy}"></div>
<div th:replace="~{footer :: copy}"></div> 
<div th:include="~{footer :: copy}"></div>
  • 三种不同的各式
    • ~{templatename::selector},指定选择器selector的模版
    • ~{templatename},整个模版
    • ~{::selector} ,同一模版中匹配指定的选择器片段
    • 删除模版
<tbody th:remove="all-but-first"></tbody>
  • 5种不同的删除方式
    • all : 删除所有
    • body: 不删除包含的标签,删除所有的子项
    • tag: 删除包含的标签,但是不删除子项
    • all-but-first:除第一个子项不删除,其他均删除
    • none: 什么都不干

工具类

  • execInfo: 有关正在处理模版的信息
  • messages: 变量表达式中获取外部化消息的方法,和#{…}语法获得的方式相同.
  • uris: 转义URL/URI的方法
  • conversions: 配置转换相关的方法
  • dates: java.util.date对象的方法: 格式化,组件提取.
  • calendars: 类似dates,calendar对象相关.
  • numbers: 用户格式化数字对象的方法.
  • strings: methods for String objects: contains, startsWith, prepending/appending, etc.
  • objects: methods for objects in general.
  • bools: methods for boolean evaluation.
  • arrays: methods for arrays.
  • lists: methods for lists.
  • sets: methods for sets.
  • maps: methods for maps.
  • aggregates: methods for creating aggregates on arrays or collections.
  • ids: methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).

其他

  • 基础语法之间都可以任意嵌套
'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
  • 默认表达式
<div th:object="${session.user}">
  ...
  <p>Age: <span th:text="*{age}?: '(no age specified)'">27</span>.</p>
</div>
  • 哑操作符,(_)表示不处理任何结果
<div th:object="${session.user}">
  ...
  <p>Age: <span th:text="*{age}?-: '(no age specified)'">27</span>.</p>
</div>
  • 数据类型转换; 变量/选择表达式的一个双括号语法,调用注册的转换服务,默认调用对象的toString方法
  • 懒加载,条件表达式为false,被迭代变量永远不会被初始化
context.setVariable(
     "users",
     new LazyContextVariable<List<User>>() {
         @Override
         protected List<User> loadValue() {
             return databaseRepository.findAllUsers();
         }
     });
context.setVariable(
     "users",
     new LazyContextVariable<List<User>>() {
         @Override
         protected List<User> loadValue() {
             return databaseRepository.findAllUsers();
         }
     });
  • 局部变量,th:with语法声明局部变量,只在包含的标签范围内使用

参考文档

  1. 官方文档

  2. thymeleaf基础语法

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值