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

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

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。

  1. @Controller
  2. @RequestMapping("/expressions")
  3. public class ExpressionsCtl {
  4. @RequestMapping("/complex")
  5. public String complex(ModelMap modeMap){
  6. // 复杂对象
  7. Family family = new Family();
  8. family.setFather(new User("father"));
  9. List<User> childList = new ArrayList<User>();
  10. childList.add(new User("son_1"));
  11. childList.add(new User("son_2"));
  12. family.setChildList(childList);
  13. modeMap.put("family", family);
  14. // map
  15. HashMap<String, User> hashMap = new HashMap<String, User>();
  16. hashMap.put("hashMapKey", new User("hashMap_name"));
  17. modeMap.put("hashMap", hashMap);
  18. return "expressions/complex";
  19. }
  20. ...
  21. }

complex.html 位于 templates/expressions/中,下方的代码都是在此html文件中

访问如下url地址,请注意url必须带上**?id=112**,否则会报错

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

2.2. 调用对象的成员变量的属性

演示如下功能

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

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

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

2.3. 调用Map的对象的属性

演示如下功能

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

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

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

2.4. 调用List的对象的属性

演示如下功能

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

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

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

2.5. 调用属性的方法

演示如下功能

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

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

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

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等属性,详细可以见源代码。

  1. <!-- #ctx是org.thymeleaf.context.IContext -->
  2. ${#ctx.locale} --> <input type="text" name="userName" th:value="${#ctx.locale}" /> <br />
  3. <!-- #ctx获取ctx的变量中的一个的值 -->
  4. ${#ctx.variables.hashMap} --> <input th:value="${#ctx.variables.hashMap}" ></input> <br />
  5. <!-- vars变量中值的一个的值,等价于#ctx.variables -->
  6. ${#ctx.variables.hashMap} --> <input th:value="${#vars.hashMap}" ></input> <br />
  7. <!-- 以下#ctx是org.thymeleaf.context.IWebContext的一个实例,他也是IContext的子类 -->
  8. <!-- #request -->
  9. ${#ctx.httpServletRequest} --> <input th:value="${#ctx.httpServletRequest}" ></input> <br />
  10. <!-- #response -->
  11. ${#ctx.httpServletResponse} --> <input th:value="${#ctx.httpServletResponse}" ></input> <br />
  12. <!-- #session -->
  13. ${#ctx.httpSession} --> <input th:value="${#ctx.httpSession}" ></input> <br />
  14. <!-- #servletContext -->
  15. ${#ctx.servletContext} --> <input th:value="${#ctx.servletContext}" ></input> <br />

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

  1. =================== 使用ctx对象: Base objects context object ===============================
  2. ${#ctx.locale} --> zh_CN
  3. ${#ctx.variables.hashMap} -->{hashMapKey=com.hry.spring.support.User@1888a92c}
  4. ${#ctx.variables.hashMap} -->{hashMapKey=com.hry.spring.support.User@1888a92c}
  5. ${#ctx.httpServletRequest} --> org.apache.catalina.connector.RequestFacade@7fdddbce
  6. ${#ctx.httpServletResponse} --> org.apache.catalina.connector.ResponseFacade@6e632047
  7. ${#ctx.httpSession} -->
  8. ${#ctx.servletContext} --> org.apache.catalina.core.ApplicationContextFacade@4149c8a4

2.7 param:获取request的请求参数

演示如下功能

  • param是org.thymeleaf.context.WebRequestParamsVariablesMap的子,封装了请求参数,下面演示param.size(),param.containsKey(‘id’),param.get(‘id’)[0]的用法。
  1. <!-- param是org.thymeleaf.context.WebRequestParamsVariablesMap的子类 -->
  2. <!-- 如果 :http://localhost:8080/expressions/complex?id=1,此时有有一个参数输出1 -->
  3. ${param.size()} --> <input th:value="${param.size()}" ></input> <br />
  4. ${param.containsKey('id')} --> <input th:value="${param.containsKey('id')}" ></input> <br />
  5. ${param.get('id')[0]} --> <input th:value="${param.get('id')[0]}" ></input> <br />
  6. 等价于:<br/>
  7. ${#ctx.httpServletRequest.getParameter('id')} --> <input th:value="${#ctx.httpServletRequest.getParameter('id')}" ></input> <br />

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

  1. ================= param:获取request的请求参数 ===============================
  2. ${param.size()} --> 1
  3. ${param.containsKey('id')} --> true
  4. ${param.get('id')[0]} --> 112
  5. 等价于:
  6. ${#ctx.httpServletRequest.getParameter('id')} --> 112

2.8. session:获取session的属性值

演示如下功能

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

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

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

2.9 application:获取application的属性值

演示如下功能

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

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

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

3. 代码

详细见Github

相关文章