Thymeleaf中的context对象-原标题:Context Objects in Thymeleaf
来源:互联网 发布:淘宝图片压缩工具 编辑:程序博客网 时间:2024/05/21 08:03
接着上一篇的看。。。。这次翻译的是 context对象
进入主题:
Thymeleaf中的context对象
在前面的文章中,我已经解释过了thymeleaf框架中不同类型的表达式。如果你是在JSP或其他web框架中工作,那么就有一些内含的上下文对象(context object)可以用。这些对象是由容器创建的,并让开发人员在所工作的上下文中轻松地调用。thymeleaf包含但不仅仅有session,request等这些基本的对象,其中还有很多工具对象。这个指导文档将会说明在thymeleaf框架中如何使用这些对象。
From the Thymeleaf Reference Docs: 来自官方文档:
表达式基本对象
- #ctx: 上下文对象.
- #vars: context中的变量们.
- #locale: context中的locale.
- #httpServletRequest: (只在web context中) HttpServletRequest对象.
- #httpSession: (只在web context中) HttpSession对象.
表达式工具对象
- #dates: java.util.Date的一个工具:包括格式化部分,提取部分日期等等。
- #calendars: 和 #dates类似, 但是是对 java.util.Calendar 的。
- #numbers: 格式化数值的工具。
- #strings: String对象的工具,包括的方法:contains, startsWith, prepending/appending等等。
- #objects: 操作object的工具。
- #bools: boolean的工具。
- #arrays: 数组的工具。
- #lists: list的工具。
- #sets: set的工具。(set是“集”的意思)
- #maps: map的工具。
- #aggregates: 用来创建数组或collection的合计的工具。(貌似是统计函数)
- #messages: 用来获取外部的message变量中信息的工具。和使用#{。。。}是一样的。
- #ids: 用来处理可能存在循环的id属性(例如,作为循环的结果)。
1. 安装 Thymeleaf 应用
为了演示如何使用context变量,我会使用我们之前的那个hello world例子(hello world example)。使用同一个例子,然后替换我们的代码。
2. 添加context变量
ThymeleafHelloWorldExample.javapackage javabeat.net.thymeleaf;import java.io.IOException;import java.util.ArrayList;import java.util.Calendar;import java.util.TreeSet;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.thymeleaf.TemplateEngine;import org.thymeleaf.context.WebContext;import org.thymeleaf.templateresolver.ServletContextTemplateResolver;public class ThymeleafHelloWorldExample extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); // XHTML is the default mode, but we will set it anyway for better understanding of code templateResolver.setTemplateMode("XHTML"); templateResolver.setPrefix("/WEB-INF/"); templateResolver.setSuffix(".html"); templateResolver.setCacheTTLMs(3600000L); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); WebContext ctx = new WebContext(req, resp, getServletConfig().getServletContext(), req.getLocale()); ctx.setVariable("today", Calendar.getInstance()); ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Value Sample 1"); arrayList.add("Value Sample 2"); TreeSet<String> set = new TreeSet<String>(); set.add("Set Sample 1"); set.add("Set Sample 2"); set.add("Set Sample 3"); ctx.setVariable("contextValue", "Store Context Value"); ctx.setVariable("listExample", arrayList); ctx.setVariable("setExample", set); req.getSession().setAttribute("sessionValue", "Store Session Value"); // This will be prefixed with /WEB-INF/ and suffixed with .html templateEngine.process("thymeleaf", ctx, resp.getWriter()); resp.setContentType("text/html;charset=UTF-8"); resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setDateHeader("Expires", 1000);}}
3. 在模板中使用context变量
thymeleaf.html<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd"><html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"><head><title>Hello World Thymeleaf!!</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><p>Session value: <span th:text="${session.sessionValue}">No Value</span></p><p>Locale country:<span th:text="${#locale.country}">US</span></p><p>Today is: <span th:text="${#calendars.format(today,'dd MMMM yyyy')}">Empty</span></p><p>Is Empty String: <span th:text="${#strings.isEmpty(contextValue)}">True</span></p><p>Upper Case String: <span th:text="${#strings.toUpperCase('javabeat')} ">None</span></p><p>List Size: <span th:text="${#lists.size(listExample)}">0</span></p><p>Set Size: <span th:text="${#sets.size(setExample)}">0</span></p><p>Format Number: <span th:text="${#numbers.formatInteger(100000,3,'POINT')}">0</span></p></body></html>
4. 运行应用
如果你运行了应用,会得到下面的输出结果。
点击此处here,查看完整的context变量和属性的列表。
0 0
- Thymeleaf中的context对象-原标题:Context Objects in Thymeleaf
- Thymeleaf的表达式语言-原标题:Expression Language in Thymeleaf
- Spring4-Thymeleaf-MyBatis整合Context文件
- 如何用th:attr标签在thymeleaf模板中设置属性-原标题:How To Set Attributes in Thymeleaf Template using th:attr?
- Thymeleaf
- thymeleaf
- thymeleaf
- Thymeleaf
- Thymeleaf
- Thymeleaf
- Thymeleaf
- Thymeleaf
- Thymeleaf
- Thymeleaf
- 如何使用th:each属性迭代模板-原标题:How To Use th:each For Iteration In Thymeleaf Template?
- thymeleaf自定义工具对象
- thymeleaf中的模板布局
- thymeleaf中的内联[ [ ] ]
- 解密“设计模式”
- Implementing the skip list data structure
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- TextView排版混乱或自动换行
- Assembly x64 Intro - SSE2 4x8 Load
- Thymeleaf中的context对象-原标题:Context Objects in Thymeleaf
- 谈语法
- JS与C#编码解码
- 使用Python抓取google街景照片
- 使用X-UA-Compatible来搞定IE浏览器兼容模式
- 黑马程序员——IO流
- 使用automake编译程序
- cocos2d-lua里sprite button imageview换图片
- MFC连接MySQL7.9数据库程序教程