您的位置:首页 > 移动开发 > Objective-C

Thymeleaf中的context对象-原标题:Context Objects in Thymeleaf

2015-12-16 13:37 615 查看
接着上一篇的看。。。。这次翻译的是 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.java

package 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{
@Override
protected 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变量和属性的列表。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: