Thymeleaf的表达式语言-原标题:Expression Language in Thymeleaf

原文来源:http://www.javabeat.net/expression-language-thymeleaf/

最近又开始弄前端啦,但是发现是时候更上一层楼了——那就是,自动化的前端构建。于是在网上查了查,看来前人们早就想到了这些,诸如freemarker,HTTL,velocity等等,看得我眼花缭乱,最后选中Thymeleaf,只是因为它只支持XML/HTML(个人比较喜欢功能单一的,专一才能更好嘛,哈哈。)虽然网上评论不一,但是功能单一,也许更容易上手吧。

学校万恶的网络,官网thymeleaf.org一直上不去,后来百度thymeleaf tutorial才找到了入门文档。虽然网上已经有了很多关于thymeleaf的入门指导文档。我自己翻译一遍,也更利于我自己理解。

翻译学习型过程中,我参考了这篇文章:http://www.blogjava.net/bjwulin/archive/2013/02/07/395234.html


另外,附一个下载链接:http://nchc.dl.sourceforge.net/project/thymeleaf/thymeleaf/2.1.4.RELEASE/thymeleaf-2.1.4.RELEASE-dist.zip


下面进入正题:Thymeleaf的表达式语言

    在之前的指导教程中,我已经解释过了那个简单的例子“hello world示例-开始使用thymeleaf框架写你的第一个例子”。如果你对这个新框架不熟悉,我建议你读一下之前的指导文档。当我们在web框架下工作的时候,有一点很重要,那就是保证表达式语言简单。这个指导文档说明了thymeleaf框架中各式各样“标记(syntax)”的语法。请看下面这个列表,它列出了thymeleaf框架所支持的表达式标记的类型:

  • 简单表达式
    • 变量表达式: ${…}
    • 选择标量表达式: *{…}
    • 信息表达式: #{…}
    • 链接URL表达式: @{…}
  • 字面量
    • 文本: ‘one text’, ‘Another one!’,…
    • 数字: 0, 34, 3.0, 12.3,…
    • 布尔值: true, false
    • NULL: null
    • 文本标记符号: one, sometext, main,…
  • 文本运算
    • 字符串连接: +
    • 字面量替换: |The name is ${name}|
  • 算术运算
    • 二元运算符: +, -, *, /, %
    • 减号(一元运算符): –
  • 逻辑运算
    • 二元运算符: and, or
    • 逻辑取反 (一元运算符): !, not
  • 比较和赋值
    • 比较运算符: >, <, >=, <= (gt, lt, ge, le)
    • 相等比较符: ==, != (eq, ne)
  • 条件运算符
    • If-then: (if) ? (then)
    • If-then-else: (if) ? (then) : (else)
    • Default: (value) ?: (defaultvalue)

在上面这个列表中,第一个是最重要的,需要首先理解的。


1. 变量表达式

Variable Expressions and Selection Variable Expressions work similar except that later will work different when used with seletecd objects. Look at the below example.

变量表达式和选择变量表达式有些相似,但是后者在选择了对象时,表现有些不同。请看下面的例子。

***也就是可以省略迭代元素名,而访问迭代元素中的各个key

<div th:object="${obj.userObj}">
    <p>Name: <span th:text="*{firstName}">Krishna</span>.</p>
    <p>Surname: <span th:text="*{lastName}">Srinivasan</span>.</p>
    <p>Nationality: <span th:text="*{country}">India</span>.</p>
  </div>

这和下面的是等价的:

<div>
    <p>Name: <span th:text="*{obj.userObj.firstName}">Krishna</span>.</p>
    <p>Surname: <span th:text="*{obj.userObj.lastName}">Srinivasan</span>.</p>
    <p>Nationality: <span th:text="*{obj.userObj.country}">India</span>.</p>
  </div>
We can use dollar and asterisk syntax together. From the above example, you could notice that instead of asterisk, you can even use the dollar syntax.
我们可以同时使用 $ 和 * 两个符号。在上面这个例子中,你应该注意到了,你可以使用美元符号来替换星号。

2. 信息表达式

thymeleaf很容易从一堆资源中获取一个message。

***此处指的是从各种.properties配置文件中获取值,例如下面这个hello,就是在.properties文件中的一个key

<p th:text="#{hello}">Hello World Thymeleaf Offline!!</p>
The above “hello” key is taken from message properties file. Also you can use the dynamic values in the messages.

上面的这个“hello”是properties文件中的一个key。那么你也就可以在网页中使用动态的值。

在.properties文件中是这么写的:

hello=Welcome {0}, Hello World Thymeleaf!!

有了上面这个属性配置,你就可以像下面这样来调用参数:

<p th:text="#{hello(${userObj.userName})}}">Hello World Thymeleaf Offline!!</p>
***此处没太明白,因为中文乱码的问题,此处使用了两个,#和$,也就是动态的从properties文件中获取值。我在想是不是这样:例如,我在properties文件中有key叫做helloMike,值为 你好麦克,另一个key叫做helloNick,值为 你好尼克。那么在不同人访问的时候,出现的东西就不同了。

3. URL表达式

Thymeleaf Standard Dialect has a special syntax for the links, the @ syntax: @{…}. Thymeleaf can handle absolute URLs like http://www.thymeleaf.org can handle at any situation. But for the relative URLs, we have specify the context path. th:href is used for setting the href attribute of the link element. Look at the example snippet below.

thymeleaf的标准方言中,对 链接URL有一个特定的标记,@符号。thymeleaf可以在任何情况下处理URL绝对路径,例如:http://www.thymeleaf.org。但是相对路径,我们需要指定一个上下文路径。th:href属性是用来设置链接元素href属性的(例如<a>标签)。请看下面的例子。

Absolute URL 绝对路径

<a href="user.html" th:href="@{http://localhost:8080/user/details(userId=${o.id})}">View User</a>
Relative URL 相对路径

在绝对路径的例子中,上下文路径被添加在最终URL上。

<a href="user.html" th:href="@{/user/details(userId=${o.id})}">View User</a>
我希望这篇文章为大家说明一个使用Thymeleaf表达式的基本思想。其他类型的表达式并不是新东西,其他的地方也常常见到,也非常容易使用,所以我就不在此赘述。







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值