Thymeleaf教程 (三) 创建一个多语言的首页
2016-09-07 16:02
190 查看
一个多语言的首页
我们要创建一个多语言的首页。第一个版本我们将编写的页面将极其简单:只是一个标题和一个欢迎信息。这是我们的/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。
环境
让我们回到第二课创建的HomeControllerpublic 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=欢迎光临<b>此中有蹊跷</b> 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表达式的语言,此语言将会解析上下文环境中的变量。
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- i-jetty环境搭配与编译
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器