本节主要介绍thymeleaf的语法:简单表达式。包括以下内容:
User
public class User {
private String name;
private boolean isAdmin;
private String other;
private int age;
// set/get 略
}
Family
public class Family {
private User father;
private List<User> childList;
// set/get 略
}
ExpressionsCtl:Control类
此类初始化测试类,当访问此URL,并转到expressions/simple.html。
@Controller
@RequestMapping("/expressions")
public class ExpressionsCtl {
/** * 简单表达式 */
@RequestMapping("/simple")
public String simpleExpressions(ModelMap map){
// 变量表达式:Variable Expressions
User user = new User("simple_name");
user.setAge(new Random().nextInt(100));
map.put("user", user);
return "expressions/simple";
}
...
}
下面的代码都在此simple.html页面中。
实现功能:
==================== 变量表达式(Variable Expressions) ===============================<br/>
<!-- 变量表达式(Variable Expressions)-->
${user.name} --> <input type="text" name="userName" value="James Carrot" th:value="${user.name}" /> <br />
<!-- 变量值和字符串拼接 -->
'The name of the user is ' + ${user.name} --> <span th:text="'The name of the user is ' + ${user.name}" ></span> <br />
<br />
输出: “–>”的左边是语法,右边是对应的输出
==================== 变量表达式(Variable Expressions) ===============================
${user.name} --> simple_name
'The name of the user is ' + ${user.name} --> The name of the user is simple_name
实现功能:
<!-- 消息表达式:Message Expressions -->
<!-- 直接从属性文件中获取值 -->
#{home.welcome} --> <span th:text="#{home.welcome}">Welcome to our grocery store!</span> <br />
<!-- 从属性文件中获取值,并替换占位符 -->
#{home.welcome.replace(${user.name})} --> <span th:text="#{home.welcome.replace(${user.name})}"></span> <br />
<br />
输出: “–>”的左边是语法,右边是对应的输出
=====================消息表达式:Message Expressions ================================= #{home.welcome} --> welcome thymeleaf
#{home.welcome.replace(${user.name})} --> welcome thymeleaf, simple_name!
实现功能:
========================== Literals =======================================<br/>
<!-- 文本 Text literals: Text literals are just character strings specified between single quotes -->
<div>
Now you are looking at a <span th:text="'working web application'">template file</span>.
</div>
<!-- 数字 Number literals: 0, 34, 3.0, 12.3,… -->
<div>仅仅输出数字 th:text="2013" --> <span th:text="2013">1492</span>.</div>
<div>数字计算 th:text="2013 + 2" --> <span th:text="2013 + 2">1494</span>.</div>
<!-- 布尔值: Boolean literals: true, false -->
${user.isAdmin()} == false --> <span th:if="${user.isAdmin()} == false"> false </span> <br />
<!-- Null值: Null literal: null -->
${user.other} == null --> <span th:if="${user.other} == null"> null</span>
<br />
输出: “–>”的左边是语法,右边是对应的输出
========================== Literals =======================================
Now you are looking at a working web application.
仅仅输出数字 th:text="2013" --> 2013.
数字计算 th:text="2013 + 2" --> 2015.
${user.isAdmin()} == false --> false
${user.other} == null --> null
实现功能:
<!-- +:字符串拼接字体串 -->
'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/>
<!-- 简化字符中拼接操作: Literal substitutions(使用"|"包围字符串,不需要对字符串使用"'") -->
|Welcome to our application, ${user.name}!| --> <span th:text="|Welcome to our application, ${user.name}!|"></span> <br />
等价于这条语句:<br />
'Welcome to our application, ' + ${user.name} + '!' --> <span th:text="'Welcome to our application, ' + ${user.name} + '!'"> </span>
<br />
输出: “–>”的左边是语法,右边是对应的输出
========================== 文本操作符: Text operations: ==================================
'The name of the user is ' + ${user.name} + '_' + ${user.age} --> The name of the user is simple_name_52
|Welcome to our application, ${user.name}!| --> Welcome to our application, simple_name!
等价于这条语句:
'Welcome to our application, ' + ${user.name} + '!' --> Welcome to our application, simple_name!
实现功能:
<!-- 二进制运算符: Binary operators: +, -, *, /, % -->
${user.age} % 2 == 0 --> <span th:text="${user.age} % 2 == 0"> </span> <br />
<!-- Boolean operations: true,false, !, not -->
true --> <span th:text="true"> </span> <br />
!(${user.age} % 2 == 0 --> <span th:text="!(${user.age} % 2 == 0)"> </span> <br />
<!-- Binary operators: and, or -->
(${user.age} % 2 == 0) and true --> <span th:text="(${user.age} % 2 == 0) and true"> </span><br />
输出: “–>”的左边是语法,右边是对应的输出
========================== 算术表达式:Arithmetic operations ==================================
${user.age} % 2 == 0 --> true
true --> true
!(${user.age} % 2 == 0 --> false
(${user.age} % 2 == 0) and true --> true
实现功能:
========================== 比较操作符:Comparisons and equality ==================================<br/>
<!-- Comparators: >, <, >=, <= (gt, lt, ge, le) -->
${user.age} > 18 --> <span th:if="${user.age} > 18"> 大人 </span> <br />
<!-- Equality operators: ==, != (eq, ne) -->
${user.age} != 18 --> <span th:if="${user.age} != 18"> 大人_no_equality </span> <br />
<br />
输出: “–>”的左边是语法,右边是对应的输出
========================== 比较操作符:Comparisons and equality ==================================
${user.age} > 18 --> 大人
${user.age} != 18 --> 大人_no_equality
实现功能:
<!-- If-then: (if) ? (then) -->
${user.age}%2==0 ? 'even' --> <span th:text="${user.age}%2==0 ? 'even'"> </span> <br />
<!-- If-then-else: (if) ? (then) : (else) -->
${user.age}%2==0 ? 'even' : 'odd' --> <span th:text="${user.age}%2==0 ? 'even' : 'odd'"> </span> <br />
<!-- 如果null值,则使用?:后面的值: Default: (value) ?: (defaultvalue) -->
${user.age} ?:'18' --> <span th:text="${user.age} ?:'18'"> </span> <br />
输出: “–>”的左边是语法,右边是对应的输出
========================== 条件操作符: Conditional operators ==================================
${user.age}%2==0 ? 'even' --> even
${user.age}%2==0 ? 'even' : 'odd' --> even
${user.age} ?:'18' --> 52
详细代码见Github
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/hry2015/article/details/73141870
内容来源于网络,如有侵权,请联系作者删除!