Thymeleaf模板语法与表达式

添加依赖

 

1         <dependency>
2             <groupId>org.springframework.boot</groupId>
3             <artifactId>spring-boot-starter-thymeleaf</artifactId>
4         </dependency>
5         <dependency>
6             <groupId>org.thymeleaf</groupId>
7             <artifactId>thymeleaf</artifactId>
8         </dependency>

使用前须添加命名空间

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

 

标准表达式语法

我们将在杂货店虚拟商店的开发中稍作休息,以了解Thymeleaf标准方言中最重要的部分之一:Thymeleaf Standard Expression语法。

我们已经看到了用这种语法表达的两种类型的有效属性值:消息表达式和变量表达式:

1 <p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
2 
3 <p>Today is: <span th:text="${today}">13 february 2011</span></p>

 

但是还有更多类型的表达式,以及更多有趣的细节来了解我们已经知道的表达式。首先,让我们看一下标准表达式功能的快速摘要:

  • 简单表达式:
    • 变量表达式:${...}
    • 选择变量表达式:*{...}
    • 消息表达式:#{...}
    • 链接网址表达式:@{...}
    • 片段表达式:~{...}
  • 文字
    • 文本文本:、,...'one text''Another one!'
    • 数字文本:、 、 、 ,...0343.012.3
    • 布尔文本: ,truefalse
    • 空文字:null
    • 文字标记:、 、 ,...onesometextmain
  • 文本操作:
    • 字符串串联:+
    • 文字替换:|The name is ${name}|
  • 算术运算:
    • 二元运算符:、 、 、 、+-*/%
    • 减号(一元运算符):-
  • 布尔运算:
    • 二元运算符:,andor
    • 布尔否定(一元运算符): ,!not
  • 比较和平等:
    • 比较器: , , , (, , ,><>=<=gtltgele)
    • 相等运算符:、 (、==!=eqne)
  • 条件运算符:
    • 如果-那么:(if) ? (then)
    • if-then-else:(if) ? (then) : (else)
    • 默认:(value) ?: (defaultvalue)
  • 特殊令牌:
    • 无操作:_

所有这些功能都可以组合和嵌套:

1 'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
次序特征属性
1 片段包含 th:insert
th:replace
2 片段迭代 th:each
3 条件评估 th:if
th:unless
th:switch
th:case
4 局部变量定义 th:object
th:with
5 常规属性修改 th:attr
th:attrprepend
th:attrappend
6 特定属性修改 th:value
th:href
th:src
...
7 文本(标签正文修改) th:text
th:utext
8 片段规范 th:fragment
9 片段删除 th:remove
OrderFeatureAttributes
1 Fragment inclusion th:insert
th:replace
2 Fragment iteration th:each
3 Conditional evaluation th:if
th:unless
th:switch
th:case
4 Local variable definition th:object
th:with
5 General attribute modification th:attr
th:attrprepend
th:attrappend
6 Specific attribute modification th:value
th:href
th:src
...
7 Text (tag body modification) th:text
th:utext
8 Fragment specification th:fragment
9 Fragment removal th:remove
 
  • Simple expressions:
    • Variable Expressions: ${...}
    • Selection Variable Expressions: *{...}
    • Message Expressions: #{...}
    • Link URL Expressions: @{...}
    • Fragment Expressions: ~{...}
  • Literals
    • Text literals: , ,…'one text''Another one!'
    • Number literals: , , , ,…0343.012.3
    • Boolean literals: , truefalse
    • Null literal: null
    • Literal tokens: , , ,…onesometextmain
  • Text operations:
    • String concatenation: +
    • Literal substitutions: |The name is ${name}|
  • Arithmetic operations:
    • Binary operators: , , , , +-*/%
    • Minus sign (unary operator): -
  • Boolean operations:
    • Binary operators: , andor
    • Boolean negation (unary operator): , !not
  • Comparisons and equality:
    • Comparators: , , , (, , , ><>=<=gtltgele)
    • Equality operators: , (, ==!=eqne)
  • Conditional operators:
    • If-then: (if) ? (then)
    • If-then-else: (if) ? (then) : (else)
    • Default: (value) ?: (defaultvalue)
 
posted @ 2022-07-19 10:00  SHINEJACKXN  阅读(100)  评论(0编辑  收藏  举报