1. 概述
本文会对thymeleaf以下功能进行举例说明:
调用对象的成员变量的属性
调用Map的对象的属性
调用List的对象的属性
调用属性的方法
使用ctx对象
param:获取request的请求参数
session:获取session的属性值
application:获取application的属性值
2. 主代码
2.1. 公共类
本文的代码会使用到Thymeleaf系列二 Thymeleaf的标准表达式语法1“2.1 公共类” 中的User和Family
ExpressionsCtl:Control类
此类中complex方法初始化测试类,当访问此方法URL,并转到expressions/complex.html。
@Controller
@RequestMapping("/expressions")
public class ExpressionsCtl {
@RequestMapping("/complex")
public String complex(ModelMap modeMap){
// 复杂对象
Family family = new Family();
family.setFather(new User("father"));
List childList = new ArrayList();
childList.add(new User("son_1"));
childList.add(new User("son_2"));
family.setChildList(childList);
modeMap.put("family", family);
// map
HashMap hashMap = new HashMap();
hashMap.put("hashMapKey", new User("hashMap_name"));
modeMap.put("hashMap", hashMap);
return "expressions/complex";
}
...
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
221
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
complex.html 位于 templates/expressions/中,下方的代码都是在此html文件中
访问如下url地址,请注意url必须带上?id=112,否则会报错
http://localhost:8080/expressions/complex?id=11211
2.2. 调用对象的成员变量的属性
演示如下功能
调用对象的成员变量的属性
${family.father.name} --> 1
21
2
输出: “–>”的左边是语法,右边是对应的输出
=================== 调用对象的成员变量的属性 ===============================
${family.father.name} --> father1
21
2
2.3. 调用Map的对象的属性
演示如下功能
通过map的key从hashMap获取对象的属性name值: 可以使用”.”或者使用”[]”获取对象值
${hashMap.hashMapKey.name} -->
等价于这条语句:
${hashMap['hashMapKey'].name} -->
1
2
3
41
2
3
4
输出: “–>”的左边是语法,右边是对应的输出
=================== 调用Map的对象的属性 ===============================
${hashMap.hashMapKey.name} --> hashMap_name
等价于这条语句:
${hashMap['hashMapKey'].name} --> hashMap_name1
2
3
41
2
3
4
2.4. 调用List的对象的属性
演示如下功能
通过[0]获取List的第一个对象的属性name值
${family.childList[0].name} -->
1
2
31
2
3
输出: “–>”的左边是语法,右边是对应的输出
=================== 调用List的对象的属性 ===============================
${family.childList[0].name} --> son_11
21
2
2.5. 调用属性的方法
演示如下功能
调用属性的方法
${family.father.name.toUpperCase()} -->
1
2
31
2
3
输出: “–>”的左边是语法,右边是对应的输出
=================== 调用属性的方法 ===============================
${family.father.name.toUpperCase()} --> FATHER1
21
2
2.6. 使用ctx对象
演示如下功能
#ctx.locale: 获取#ctx的locale值
#ctx.variables: 获取#ctx的变量中的一个的值
#ctx.variables: 变量中值的一个的值,等价于#ctx.variables
#ctx.httpServletRequest:请求request
#ctx.httpServletResponse:返回response
#ctx.httpSession:返回session
#ctx.servletContext:返回servletContext
在非web的环境里,ctx只是org.thymeleaf.context.IContext的一个实例,而在web的环境里org.thymeleaf.context.IWebContext的一个实例,此接口同时是org.thymeleaf.context.IContext的子类。IWebContext比IContext多了httpServletRequest,httpServletResponse,httpSession,servletContext等属性,详细可以见源代码。
${#ctx.locale} -->
${#ctx.variables.hashMap} -->
${#ctx.variables.hashMap} -->
${#ctx.httpServletRequest} -->
${#ctx.httpServletResponse} -->
${#ctx.httpSession} -->
${#ctx.servletContext} -->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
211
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
输出: “–>”的左边是语法,右边是对应的输出
=================== 使用ctx对象: Base objects context object ===============================
${#ctx.locale} --> zh_CN
${#ctx.variables.hashMap} -->{hashMapKey=com.hry.spring.support.User@1888a92c}
${#ctx.variables.hashMap} -->{hashMapKey=com.hry.spring.support.User@1888a92c}
${#ctx.httpServletRequest} --> org.apache.catalina.connector.RequestFacade@7fdddbce
${#ctx.httpServletResponse} --> org.apache.catalina.connector.ResponseFacade@6e632047
${#ctx.httpSession} -->
${#ctx.servletContext} --> org.apache.catalina.core.ApplicationContextFacade@4149c8a41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
161
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2.7 param:获取request的请求参数
演示如下功能
param是org.thymeleaf.context.WebRequestParamsVariablesMap的子,封装了请求参数,下面演示param.size(),param.containsKey(‘id’),param.get(‘id’)[0]的用法。
${param.size()} -->
${param.containsKey('id')} -->
${param.get('id')[0]} -->
等价于:
${#ctx.httpServletRequest.getParameter('id')} -->
1
2
3
4
5
6
7
8
91
2
3
4
5
6
7
8
9
输出: “–>”的左边是语法,右边是对应的输出
================= param:获取request的请求参数 ===============================
${param.size()} --> 1
${param.containsKey('id')} --> true
${param.get('id')[0]} --> 112
等价于:
${#ctx.httpServletRequest.getParameter('id')} --> 1121
2
3
4
5
6
7
81
2
3
4
5
6
7
8
2.8. session:获取session的属性值
演示如下功能
session是org.thymeleaf.context.WebSessionVariablesMap的子类
================= session:获取session的属性值 ===============================
${session.size()} -->
1
2
3
41
2
3
4
输出: “–>”的左边是语法,右边是对应的输出
================= session:获取session的属性值 ===============================
${session.size()} --> 01
21
2
2.9 application:获取application的属性值
演示如下功能
application是org.thymeleaf.context.WebServletContextVariablesMap的子类
================= application:获取application的属性值 ========================
${application.size()} -->
1
2
3
41
2
3
4
输出: “–>”的左边是语法,右边是对应的输出
================= application:获取application的属性值 ========================
${application.size()} --> 91
21
2
3. 代码