Thymeleaf教程 (三) 创建一个多语言的首页

来源:互联网 发布:网络超市的前景 编辑:程序博客网 时间:2024/04/29 15:25

一个多语言的首页

我们要创建一个多语言的首页。

第一个版本我们将编写的页面将极其简单:只是一个标题和一个欢迎信息。这是我们的/WEBINF/templates/home.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>Good Thymes Virtual Grocery</title>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />        <link rel="stylesheet" type="text/css" media="all"              href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />    </head>    <body>        <p th:text="#{home.welcome}">Welcome to our grocery store!</p>    </body></html>

你会发现,此xhtml文件使用浏览器单独打开的话,可以被良好的显示,因为它并不包含其他非法的XHTML标签(浏览器会忽视所有它不明白的属性,比如 th:text )。

让我们看看上面代码做了什么。

首先在thymeleaf名称空间被描述为th:*属性:

<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org">

然后我们添加了 th:text=”#{home.welcome}”属性到P里。

<p th:text="#{home.welcome}">Welcome to our grocery store!</p>

在这里我们用了两种不同的Thymeleaf Standard Dialect特性:

  • ”th:text“属性,将会解析出一个结果来替换”Welcome to our grocery store!“
  • ”#{home.welcome}“表达式,表达式将会在外部环境中获取指定的KEY的文本并返回。

那么外部的文本在哪里呢?

Thymeleaf的外部化文本的位置是完全可配置的,并且它将取决于 org.thymeleaf.messageresolver。IMessageResolver的具体实现。通常我们会使用*.properties,但我们可以创造我们自己的实现,例如,从数据库中获取文本。

然而,在我们在初始化我们的模板引擎时没有指定消息解析器,这意味着我们的应用程序使用标准的消息解析器,此类会实现类org.thymeleaf.messageresolver。StandardMessageResolver

那么消息解析器会在”/WEB-INF/templates/home.html“文件的相同目录下去寻找同名的 .properties文件,举个栗子:

  • /WEB-INF/templates/home_en.properties for English texts.
  • /WEB-INF/templates/home_es.properties for Spanish language texts.
  • /WEB-INF/templates/home_pt_BR.properties for Portuguese (Brazil) language texts.
  • /WEB-INF/templates/home.properties for default texts
    (if locale is not matched).

home.properties文件中的内容如下:

home.welcome=欢迎光临本店。

以上就是我们使用Thymeleaf创建的模板,接下来是服务端的controller。

环境

让我们回到第二课创建的HomeController

public class HomeController implements IGTVGController {    public void process(HttpServletRequest request, HttpServletResponse response,                ServletContext servletContext, TemplateEngine templateEngine) {        WebContext ctx =new WebContext(request, response, servletContext, request.getLocale());        templateEngine.process("home", ctx, response.getWriter());    }}

这步将创建运行的服务端的环境。并映射路径。

templateEngine.process("home", ctx, response.getWriter());

然后运行程序。结果如下

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml">    <head>        <title>Good Thymes Virtual Grocery</title>        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>        <link rel="stylesheet" type="text/css" media="all" href="/gtvg/css/gtvg.css" />    </head>    <body>        <p>欢迎光临本店!</p>    </body></html>

更多的文本和值的特性

不需要转义的文本

home.welcome=欢迎光临 <b>此中有蹊跷</b> 本店!

此文本会最终显示为:

home.welcome=欢迎光临&lt;b&gt;此中有蹊跷&lt;/b&gt; grocery store!

对于这种不需要转义的文本,则用th:utext属性。

使用和显示变量

现在让我们添加更多的内容到我们的主页。例如,我们可能希望显示下面的日期我们的消息文本中,像这样:

Welcome to our fantastic grocery store!Today is: 12 july 2010

那么首先我们将更改controller文件,并把日期参数添加进去。

public void process(HttpServletRequest request, HttpServletResponse response,            ServletContext servletContext, TemplateEngine templateEngine) {    SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");    Calendar cal = Calendar.getInstance();    WebContext ctx =new WebContext(request, response, servletContext, request.getLocale());    ctx.setVariable("today", dateFormat.format(cal.getTime()));    templateEngine.process("home", ctx, response.getWriter());}

我们增加了一个String类型的日期。并且把此日期”today“放到模板中

<body>    <p th:utext="#{home.welcome}">Welcome to our grocery store!</p>    <p>Today is: <span th:text="${today}">13 February 2011</span></p></body>

这样就完成了。这里使用了${。。}表达试,这是一个变量表达式,它能解析OGNL表达式的语言,此语言将会解析上下文环境中的变量。

1 2
原创粉丝点击