Thymeleaf教程 (四) Thymeleaf标准表达式语法(上)

咱们已经知道了两种语法html

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

可是还有不少语法咱们不知道,接下来咱们快速的介绍更多的表达式语法:java

  • 简单表示式:算法

    • 变量表达式: ${…}
    • 选择变量表达式: *{…}
    • 信息表达式: #{…}
    • URL链接表达式: @{…}
  • 文字类型:express

    • 字符型: ‘one text’ , ‘Another one!’ ,…
    • 数值型: 0 , 34 , 3.0 , 12.3 ,…
    • Boolean型: true , false
    • 空值: null
    • 文本字符串: one , sometext , main ,…
  • 字符串操做: 
    • 字符串链接: +
    • 文字替换: |The name is ${name}|
  • 数值型操做: 
    • 运算符: + , - , * , / , %
    • 负号: -
  • Boolean操做: 
    • 运算符: and , or
    • 非运算符: ! , not
  • 比较相等算法: 
    • 比较: > , < , >= , <= ( gt , lt , ge , le )
    • 相等算法: == , != ( eq , ne )
  • 条件语句:apache

    • If-then: (if) ? (then)
    • If-then-else: (if) ? (then) : (else)
    • Default: (value) ?: (defaultvalue)

    全部上面算法均可以随意组合和嵌套:session

'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))

信息表达式

咱们以前的例子是这样的app

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
home.welcome=欢迎光临本店,

可是有的时候咱们须要在消息中增长变量,好比客人的名字怎么办?好比达到以下效果ide

<p>¡Bienvenido a nuestra tienda de comestibles, 木鱼!</p>

这样办:函数

home.welcome=欢迎光临本店, {0}!
<p th:utext="#{home.welcome(${session.user.name})}">
¡Bienvenido a nuestra tienda de comestibles, 木鱼!
</p>

在这里,参数能够是字符型也但是树数值型或者日期型。固然若是咱们须要多个参数的话,类推便可,而且咱们也能够内嵌表达式替换字符串,好比:工具

<p th:utext="#{${welcomeMsgKey}(${session.user.name})}">
Welcome to our grocery store, 木鱼!
</p>

变量表达式

变量表达式能够解析OGNL语法。详尽的语法信息能够访问官网: 
http://commons.apache.org/ognl/

系统基本对象

OGNL有如下基本内置对象

  • #ctx : the context
  • #object. vars: the context variables.
  • #locale : the context locale.
  • #httpServletRequest : (only in Web Contexts)theHttpServletRequest object.
  • #httpSession : (only in Web Contexts) the HttpSession object. 
    因此咱们能够用以下方式引用:
Established locale country: <span th:text="${#locale.country}">US</span>.

Thymeleaf提供的对象

除了这些基本的对象,Thymeleaf将为咱们提供一套实用的对象。来帮助咱们咱们执行常见的任务。

  • #dates : 为 java.util.Date对象提供工具方法,好比:格式化,提取年月日等.
  • #calendars : 相似于#dates , 可是只针对java.util.Calendar对象.
  • #numbers : 为数值型对象提供工具方法。
  • #strings :为String 对象提供工具方法。如: contains, startsWith, prepending/appending等。
  • #objects : 为object 对象提供经常使用的工具方法。
  • #bools : 为boolean 对象提供经常使用的工具方法。
  • #arrays : 为arrays 对象提供经常使用的工具方法。
  • #lists :为lists对象提供经常使用的工具方法。
  • #sets : 为sets对象提供经常使用的工具方法。
  • #maps : 为maps对象提供经常使用的工具方法。
  • #aggregates :为创造一个arrays 或者 collections汇集函数提供经常使用的工具方法。
  • #messages : utility methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax?.
  • #ids : 为可能须要循环的ID属性提供经常使用的工具方法。

在咱们的主页中从新格式化日期

如今咱们知道了Thymeleaf提供的工具类和表达式的语法,那么咱们来从新格式化首页的日期吧,首先在咱们的controller层中吧字符型日期替换成对象

SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
Calendar cal = Calendar.getInstance();
WebContext ctx = new WebContext(request, servletContext, request.getLocale());
ctx.setVariable("today", dateFormat.format(cal.getTime()));
templateEngine.process("home", ctx, response.getWriter());

替换成

WebContext ctx = new WebContext(request, servletContext, request.getLocale());
ctx.setVariable("today", Calendar.getInstance());
templateEngine.process("home", ctx, response.getWriter());

而后是模板

<p>
Today is: <span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span>
</p>

选择表达式用法(*{ })

变量不只能用在#{ }上,还能用在* { }上。二者的区别在于* { }上的的变量首先是选定对象的变量。若是不选定对象,那么是整个上下文环境中的变量和#{ }相同

选择对象用什么呢?th:object标签属性。咱们使用它在咱们的用户配置文件(userprofile.html)页面:

<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

这个用法等同于

<div>
<p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
</div>

固然,两种用法能够混合。

<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

若是一个对象已经被选择,即th:object=”${session.user}”。那么咱们也使用#object对象去引用。

<div th:object="${session.user}">
<p>Name: <span th:text="${#object.firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

就像以前说的,若是没有对象被选中,那么#{ }和* { }表达式的意义是相同的。

<div>
<p>Name: <span th:text="*{session.user.name}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{session.user.surname}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{session.user.nationality}">Saturn</span>.</p>
</div>
相关文章
相关标签/搜索