本节主要介绍thymeleaf的语法:简单表达式。包括以下内容:
- 变量表达式:Variable Expressions
- 消息表达式:Message Expressions
- Literals
- 文本操作符: Text operations
- 算术表达式:Arithmetic operations
- 比较操作符:Comparisons and equality
- 条件操作符: Conditional operators
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 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/>
${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
实现功能:
#{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/>
<div>
Now you are looking at a <span th:text="'working web application'">template filespan>.
div>
<div>仅仅输出数字 th:text="2013" --> <span th:text="2013">1492span>.div>
<div>数字计算 th:text="2013 + 2" --> <span th:text="2013 + 2">1494span>.div>
${user.isAdmin()} == false --> <span th:if="${user.isAdmin()} == false"> false span> <br />
${user.other} == null --> <span th:if="${user.other} == null"> nullspan>
<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/>
|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!
实现功能:
${user.age} % 2 == 0 --> <span th:text="${user.age} % 2 == 0"> span> <br />
true --> <span th:text="true"> span> <br />
!(${user.age} % 2 == 0 --> <span th:text="!(${user.age} % 2 == 0)"> span> <br />
(${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/>
${user.age} > 18 --> <span th:if="${user.age} > 18"> 大人 span> <br />
${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
实现功能:
${user.age}%2==0 ? 'even' --> <span th:text="${user.age}%2==0 ? 'even'"> span> <br />
${user.age}%2==0 ? 'even' : 'odd' --> <span th:text="${user.age}%2==0 ? 'even' : 'odd'"> span> <br />
${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