博主
258
258
258
258
专辑

第八节 Thymeleaf模版指令:调用对象的成员变量值

亮子 2022-02-16 01:14:19 6258 0 0 0

Thymeleaf调用对象的成员变量值、Map值、List值、属性的方法 、ctx对象、param、session和application

一、概述

本文会对thymeleaf以下功能进行举例说明:

• 调用对象的成员变量的属性
• 调用Map的对象的属性
• 调用List的对象的属性
• 调用属性的方法
• 使用ctx对象
• param:获取request的请求参数
• session:获取session的属性值
• application:获取application的属性值

二、实例说明

1、控制层代码

此类中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<User> childList = new ArrayList<User>();
        childList.add(new User("son_1"));
        childList.add(new User("son_2"));
        family.setChildList(childList);
        modeMap.put("family", family);
        // map
        HashMap<String, User> hashMap = new HashMap<String, User>();
        hashMap.put("hashMapKey", new User("hashMap_name"));
        modeMap.put("hashMap", hashMap);

        return "expressions/complex";
    }
    // ...
}

complex.html 位于 templates/expressions/中,下方的代码都是在此html文件中
访问如下url地址,请注意url必须带上?id=112,否则会报错

http://localhost:8080/expressions/complex?id=112

3、调用对象的成员变量的属性

演示如下功能

  • 调用对象的成员变量的属性
<!-- 获取family的成员变量father的属性 -->
${family.father.name} --> <input type="text" name="userName" th:value="${family.father.name}" />

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

=================== 调用对象的成员变量的属性 ===============================
${family.father.name} --> father

3、调用Map的对象的属性

演示如下功能

通过map的key从hashMap获取对象的属性name值: 可以使用”.”或者使用”[]”获取对象值

<!-- 通过map的key从hashMap获取对象的属性name值: 可以使用"."或者使用"[]"获取对象值 -->
${hashMap.hashMapKey.name} --> <input type="text" name="userName" th:value="${hashMap.hashMapKey.name}" /> <br />
等价于这条语句:
${hashMap['hashMapKey'].name} --> <input type="text" name="userName" th:value="${hashMap['hashMapKey'].name}" /> <br />

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

=================== 调用Map的对象的属性 ===============================
${hashMap.hashMapKey.name} --> hashMap_name
等价于这条语句: 
${hashMap['hashMapKey'].name} --> hashMap_name

4、调用List的对象的属性

演示如下功能

通过[0]获取List的第一个对象的属性name值

<!-- 通过[0]获取List的第一个对象的属性name值 -->
${family.childList[0].name} --> <input type="text" name="userName" th:value="${family.childList[0].name}" /> <br />
<br />

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

=================== 调用List的对象的属性 ===============================
${family.childList[0].name} --> son_1

5、调用属性的方法

演示如下功能

调用属性的方法

<!-- 调用属性的方法 -->
${family.father.name.toUpperCase()} --> <input type="text" name="userName" th:value="${family.father.name.toUpperCase()}" /> <br />

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

=================== 调用属性的方法 ===============================
${family.father.name.toUpperCase()} --> FATHER

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是org.thymeleaf.context.IContext -->
${#ctx.locale} --> <input type="text" name="userName" th:value="${#ctx.locale}" /> <br />

<!-- #ctx获取ctx的变量中的一个的值  -->
${#ctx.variables.hashMap} --> <input th:value="${#ctx.variables.hashMap}" ></input> <br />

<!-- vars变量中值的一个的值,等价于#ctx.variables -->
${#ctx.variables.hashMap} --> <input th:value="${#vars.hashMap}" ></input> <br />

<!-- 以下#ctx是org.thymeleaf.context.IWebContext的一个实例,他也是IContext的子类 -->
<!-- #request -->
${#ctx.httpServletRequest} --> <input th:value="${#ctx.httpServletRequest}" ></input> <br />

<!-- #response -->
${#ctx.httpServletResponse} --> <input th:value="${#ctx.httpServletResponse}" ></input> <br />

<!-- #session -->
${#ctx.httpSession} --> <input th:value="${#ctx.httpSession}" ></input> <br />

<!-- #servletContext -->
${#ctx.servletContext} --> <input th:value="${#ctx.servletContext}" ></input> <br />

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

=================== 使用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@4149c8a4

7、param:获取request的请求参数

演示如下功能

param是org.thymeleaf.context.WebRequestParamsVariablesMap的子,封装了请求参数,下面演示param.size(),param.containsKey(‘id’),param.get(‘id’)[0]的用法。

<!-- param是org.thymeleaf.context.WebRequestParamsVariablesMap的子类 -->
<!-- 如果 :http://localhost:8080/expressions/complex?id=1,此时有有一个参数输出1 -->
${param.size()} --> <input th:value="${param.size()}" ></input> <br />
${param.containsKey('id')} --> <input th:value="${param.containsKey('id')}" ></input> <br />

${param.get('id')[0]} --> <input th:value="${param.get('id')[0]}" ></input> <br />
等价于:<br/>
${#ctx.httpServletRequest.getParameter('id')} --> <input th:value="${#ctx.httpServletRequest.getParameter('id')}" ></input> <br />

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

================= param:获取request的请求参数 ===============================
${param.size()} --> 1

${param.containsKey('id')} --> true

${param.get('id')[0]} --> 112
等价于:
${#ctx.httpServletRequest.getParameter('id')} --> 112

8、session:获取session的属性值

演示如下功能

session是org.thymeleaf.context.WebSessionVariablesMap的子类

================= session:获取session的属性值  ===============================<br/>
<!-- session是org.thymeleaf.context.WebSessionVariablesMap的子类 -->
${session.size()} --> <input th:value="${session.size()}" ></input> <br />

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

================= session:获取session的属性值 ===============================
${session.size()} --> 0

9、application:获取application的属性值

演示如下功能

application是org.thymeleaf.context.WebServletContextVariablesMap的子类

================= application:获取application的属性值  ========================<br/>
<!-- application是org.thymeleaf.context.WebServletContextVariablesMap的子类 -->
${application.size()} --> <input th:value="${application.size()}" ></input> <br />

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

================= application:获取application的属性值 ========================
${application.size()} --> 9