本文会对thymeleaf以下功能进行举例说明:
本文的代码会使用到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";
}
...
}
complex.html 位于 templates/expressions/中,下方的代码都是在此html文件中
访问如下url地址,请注意url必须带上?id=112,否则会报错
http://localhost:8080/expressions/complex?id=112
演示如下功能
${family.father.name} --> <input type="text" name="userName" th:value="${family.father.name}" />
输出: “–>”的左边是语法,右边是对应的输出
=================== 调用对象的成员变量的属性 ===============================
${family.father.name} --> father
演示如下功能
${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
演示如下功能
${family.childList[0].name} --> <input type="text" name="userName" th:value="${family.childList[0].name}" /> <br />
<br />
输出: “–>”的左边是语法,右边是对应的输出
=================== 调用List的对象的属性 ===============================
${family.childList[0].name} --> son_1
演示如下功能
${family.father.name.toUpperCase()} --> <input type="text" name="userName" th:value="${family.father.name.toUpperCase()}" /> <br />
输出: “–>”的左边是语法,右边是对应的输出
=================== 调用属性的方法 ===============================
${family.father.name.toUpperCase()} --> FATHER
演示如下功能
在非web的环境里,ctx只是org.thymeleaf.context.IContext的一个实例,而在web的环境里org.thymeleaf.context.IWebContext的一个实例,此接口同时是org.thymeleaf.context.IContext的子类。IWebContext比IContext多了httpServletRequest,httpServletResponse,httpSession,servletContext等属性,详细可以见源代码。
${#ctx.locale} --> <input type="text" name="userName" th:value="${#ctx.locale}" /> <br />
${#ctx.variables.hashMap} --> <input th:value="${#ctx.variables.hashMap}" >input> <br />
${#ctx.variables.hashMap} --> <input th:value="${#vars.hashMap}" >input> <br />
${#ctx.httpServletRequest} --> <input th:value="${#ctx.httpServletRequest}" >input> <br />
${#ctx.httpServletResponse} --> <input th:value="${#ctx.httpServletResponse}" >input> <br />
${#ctx.httpSession} --> <input th:value="${#ctx.httpSession}" >input> <br />
${#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
演示如下功能
${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
演示如下功能
================= session:获取session的属性值 ===============================<br/>
${session.size()} --> <input th:value="${session.size()}" >input> <br />
输出: “–>”的左边是语法,右边是对应的输出
================= session:获取session的属性值 ===============================
${session.size()} --> 0
演示如下功能
================= application:获取application的属性值 ========================<br/>
${application.size()} --> <input th:value="${application.size()}" >input> <br />
输出: “–>”的左边是语法,右边是对应的输出
================= application:获取application的属性值 ========================
${application.size()} --> 9
详细见Github