Thymeleaf 学习笔记 (1)

本文详细介绍了Thymeleaf模板引擎的使用方法,包括标准表达式的语法、标签属性的功能及应用场景等,并提供了实例说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

标准表达式语法


  • message 国际化资源文件内容语法:#{}

    • 没有参数的:#{home.welcome}
    • 带有参数的:#{home.welcome(${username})}

  • 变量语法:${}
  • 选中对象内语法:*{}
  • 链接语法:@{}

    • @{user/login} :相对页面路径
    • @{/user/login}:相对Context 路径
    • @{~/user/login}:相对 Server 路径
    • @{http://www.baidu.com}: 绝对路径
    • @{//js.baidu.com/test.js}:相同协议的绝对路径
  • 片段语法:~{}

表达式基本对象:


  • #ctx:上下文对象
  • #vars:上下文中的变量集合,类似于 key,value 结构吧
  • #locale:上下文语言环境变量
  • #request
  • #response
  • #session
  • #servletContext
     

表达式工具对象:

 

  • #exeInfo

  • #messages
  • #uris
  • #conversions
  • #dates
  • #calendars
  • #numbers
  • #strings
  • #objects
  • #bools
  • #arrays
  • #lists
  • #sets
  • #maps
  • #aggregates
  • #ids
     

标签属性:


  • th:text
  • th:remove

    • all
    • body
    • tag
    • all-but-first
    • none
       

  • th:utext
  • th:assert
  • th:insert
  • th:replace
  • th:if:判断if 条件的,配合使用LazyContextVariable 对象可以增强性能
    • th:if="${not #lists.isEmpty(prod.comments)} 
  • th:with:好像是赋值变量的,th:with="name=${username}"
  • th:unless:与 if 的意思相反
  • th:each:循环

    • 循环集合:th:each="prod : ${prods}"状态变量:th:each="prod,iterState : ${prods}",iterState(如果不指定名称,默认是单个对象的变量名后加 Stat,这个例子中是prodStat)中有以下属性
      • index:循环下标,0开始
      • count: 第几个,1开始
      • size:共几个
      • current:当前的对象
      • even:是否是单数行
      • odd:是否是双数行
      • first:第一个吗?boolean
      • last:最后一个吗?boolean
  • th:switch:多个值与此进行对比如果相同则执行,th:switch="${user.role}"
    • th:case:只要一个 case 为 true 其他的都为 false ,指定默认设置th:case="*"
  • th:attr
    • 设置单个属性:th:attr="src@{/images/1.png}"
    • 设置多个属性:th:attr="src@{/images/1.png},title=#{appTitle}"
  • th:attrappend:在指定属性原值之后追加,th:attrapppend="class=${\' \' +cssClassName}"
    • th:classappend:在 class 后追加
    • th:styleappend:在 style 后追加
  • th:attrprepend:在指定属性原值之前追加,th:attrprepend="name=${\'obj.\'}"

    • 有待测试
       
  • th:checked:动态增加 checked 属性和值,true 则选中,false 则移除 checked 属性,th:checked="${user.active}"
  • th:* : 设置任意的属性值

注意:标签内容替换需要unescape 的使用th:utext,需要 escape 的使用th:text。

标签

  • th:block

转载于:https://my.oschina.net/yangyan/blog/811733

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值