Thymeleaf系列二 简单表达式: 变量、消息、Literals、文本、算术、比较和条件表达式

x33g5p2x  于2021-12-24 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(462)

1. 概述

本节主要介绍thymeleaf的语法:简单表达式。包括以下内容:

  • 变量表达式:Variable Expressions
  • 消息表达式:Message Expressions
  • Literals
  • 文本操作符: Text operations
  • 算术表达式:Arithmetic operations
  • 比较操作符:Comparisons and equality
  • 条件操作符: Conditional operators

2. 例子

2.1 公共类

User

  1. public class User {
  2. private String name;
  3. private boolean isAdmin;
  4. private String other;
  5. private int age;
  6. // set/get 略
  7. }

Family

  1. public class Family {
  2. private User father;
  3. private List<User> childList;
  4. // set/get 略
  5. }

ExpressionsCtl:Control类
此类初始化测试类,当访问此URL,并转到expressions/simple.html。

  1. @Controller
  2. @RequestMapping("/expressions")
  3. public class ExpressionsCtl {
  4. /** * 简单表达式 */
  5. @RequestMapping("/simple")
  6. public String simpleExpressions(ModelMap map){
  7. // 变量表达式:Variable Expressions
  8. User user = new User("simple_name");
  9. user.setAge(new Random().nextInt(100));
  10. map.put("user", user);
  11. return "expressions/simple";
  12. }
  13. ...
  14. }

下面的代码都在此simple.html页面中。

2.2 变量表达式(Variable Expressions)

实现功能:

  • 简单的表达式
  • 变量值和字符串拼接
  1. ==================== 变量表达式(Variable Expressions) ===============================<br/>
  2. <!-- 变量表达式(Variable Expressions)-->
  3. ${user.name} --> <input type="text" name="userName" value="James Carrot" th:value="${user.name}" /> <br />
  4. <!-- 变量值和字符串拼接 -->
  5. 'The name of the user is ' + ${user.name} --> <span th:text="'The name of the user is ' + ${user.name}" ></span> <br />
  6. <br />

输出: “–>”的左边是语法,右边是对应的输出

  1. ==================== 变量表达式(Variable Expressions) ===============================
  2. ${user.name} --> simple_name
  3. 'The name of the user is ' + ${user.name} --> The name of the user is simple_name

2.3 消息表达式:Message Expressions

实现功能:

  • 直接从属性文件中获取值
  • 从属性文件中获取值,并替换占位符
  1. <!-- 消息表达式:Message Expressions -->
  2. <!-- 直接从属性文件中获取值 -->
  3. #{home.welcome} --> <span th:text="#{home.welcome}">Welcome to our grocery store!</span> <br />
  4. <!-- 从属性文件中获取值,并替换占位符 -->
  5. #{home.welcome.replace(${user.name})} --> <span th:text="#{home.welcome.replace(${user.name})}"></span> <br />
  6. <br />

输出: “–>”的左边是语法,右边是对应的输出

  1. =====================消息表达式:Message Expressions ================================= #{home.welcome} --> welcome thymeleaf
  2. #{home.welcome.replace(${user.name})} --> welcome thymeleaf, simple_name!

2.4 Literals

实现功能:

  • 文本 Text literals
  • 数字 Number literals
  • 布尔值
  • Null值
  1. ========================== Literals =======================================<br/>
  2. <!-- 文本 Text literals: Text literals are just character strings specified between single quotes -->
  3. <div>
  4. Now you are looking at a <span th:text="'working web application'">template file</span>.
  5. </div>
  6. <!-- 数字 Number literals: 0, 34, 3.0, 12.3,… -->
  7. <div>仅仅输出数字 th:text="2013" --> <span th:text="2013">1492</span>.</div>
  8. <div>数字计算 th:text="2013 + 2" --> <span th:text="2013 + 2">1494</span>.</div>
  9. <!-- 布尔值: Boolean literals: true, false -->
  10. ${user.isAdmin()} == false --> <span th:if="${user.isAdmin()} == false"> false </span> <br />
  11. <!-- Null值: Null literal: null -->
  12. ${user.other} == null --> <span th:if="${user.other} == null"> null</span>
  13. <br />

输出: “–>”的左边是语法,右边是对应的输出

  1. ========================== Literals =======================================
  2. Now you are looking at a working web application.
  3. 仅仅输出数字 th:text="2013" --> 2013.
  4. 数字计算 th:text="2013 + 2" --> 2015.
  5. ${user.isAdmin()} == false --> false
  6. ${user.other} == null --> null

2.5 文本操作符: Text operations

实现功能:

  • +:字符串拼接字体串
  • 简化字符中拼接操作: Literal substitutions(使用”|”包围字符串,不需要对字符串使用”’”)
  1. <!-- +:字符串拼接字体串 -->
  2. 'The name of the user is ' + ${user.name} + '_' + ${user.age} --> <span th:text="'The name of the user is ' + ${user.name} + '_' + ${user.age}"> </span> <br/>
  3. <!-- 简化字符中拼接操作: Literal substitutions(使用"|"包围字符串,不需要对字符串使用"'") -->
  4. |Welcome to our application, ${user.name}!| --> <span th:text="|Welcome to our application, ${user.name}!|"></span> <br />
  5. 等价于这条语句:<br />
  6. 'Welcome to our application, ' + ${user.name} + '!' --> <span th:text="'Welcome to our application, ' + ${user.name} + '!'"> </span>
  7. <br />

输出: “–>”的左边是语法,右边是对应的输出

  1. ========================== 文本操作符: Text operations: ==================================
  2. 'The name of the user is ' + ${user.name} + '_' + ${user.age} --> The name of the user is simple_name_52
  3. |Welcome to our application, ${user.name}!| --> Welcome to our application, simple_name!
  4. 等价于这条语句:
  5. 'Welcome to our application, ' + ${user.name} + '!' --> Welcome to our application, simple_name!

2.6 算术表达式:Arithmetic operations

实现功能:

  • 二进制运算符: Binary operators: +, -, *, /, %
  • Boolean operations: true,false, !, not
  • Binary operators: and, or
  1. <!-- 二进制运算符: Binary operators: +, -, *, /, % -->
  2. ${user.age} % 2 == 0 --> <span th:text="${user.age} % 2 == 0"> </span> <br />
  3. <!-- Boolean operations: true,false, !, not -->
  4. true --> <span th:text="true"> </span> <br />
  5. !(${user.age} % 2 == 0 --> <span th:text="!(${user.age} % 2 == 0)"> </span> <br />
  6. <!-- Binary operators: and, or -->
  7. (${user.age} % 2 == 0) and true --> <span th:text="(${user.age} % 2 == 0) and true"> </span><br />

输出: “–>”的左边是语法,右边是对应的输出

  1. ========================== 算术表达式:Arithmetic operations ==================================
  2. ${user.age} % 2 == 0 --> true
  3. true --> true
  4. !(${user.age} % 2 == 0 --> false
  5. (${user.age} % 2 == 0) and true --> true

2.7 比较操作符:Comparisons and equality

实现功能:

  • 比较符号Comparators: >, <, >=, <= (gt, lt, ge, le)
  • Equality operators: ==, != (eq, ne)
  1. ========================== 比较操作符:Comparisons and equality ==================================<br/>
  2. <!-- Comparators: >, <, >=, <= (gt, lt, ge, le) -->
  3. ${user.age} &gt; 18 --> <span th:if="${user.age} &gt; 18"> 大人 </span> <br />
  4. <!-- Equality operators: ==, != (eq, ne) -->
  5. ${user.age} != 18 --> <span th:if="${user.age} != 18"> 大人_no_equality </span> <br />
  6. <br />

输出: “–>”的左边是语法,右边是对应的输出

  1. ========================== 比较操作符:Comparisons and equality ==================================
  2. ${user.age} > 18 --> 大人
  3. ${user.age} != 18 --> 大人_no_equality

2.8 条件操作符: Conditional operators

实现功能:

  • If-then: (if) ? (then)
  • If-then-else: (if) ? (then) : (else)
  • 如果null值,则使用?:后面的值: Default: (value) ?: (defaultvalue)
  1. <!-- If-then: (if) ? (then) -->
  2. ${user.age}%2==0 ? 'even' --> <span th:text="${user.age}%2==0 ? 'even'"> </span> <br />
  3. <!-- If-then-else: (if) ? (then) : (else) -->
  4. ${user.age}%2==0 ? 'even' : 'odd' --> <span th:text="${user.age}%2==0 ? 'even' : 'odd'"> </span> <br />
  5. <!-- 如果null值,则使用?:后面的值: Default: (value) ?: (defaultvalue) -->
  6. ${user.age} ?:'18' --> <span th:text="${user.age} ?:'18'"> </span> <br />

输出: “–>”的左边是语法,右边是对应的输出

  1. ========================== 条件操作符: Conditional operators ==================================
  2. ${user.age}%2==0 ? 'even' --> even
  3. ${user.age}%2==0 ? 'even' : 'odd' --> even
  4. ${user.age} ?:'18' --> 52

3. 代码

详细代码见Github

相关文章