Thymeleaf教程(4) - Thymeleaf标准表达式语法(上)
发布于 2022-05-01    455 次阅读
我们已经知道了两种语法 <p th:utext="#{home.welcome}">Welcome to our grocery store!</p> <p>Today is: <span th:text="${today}">13 february 2011</span></p> 但是还有很多语法我们不知道,接下来我们快速的介绍更多的表达式语法: 简单表示式: 变量表达式: ${…} 选择变量表...

我们已经知道了两种语法

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

但是还有很多语法我们不知道,接下来我们快速的介绍更多的表达式语法:

  • 简单表示式:

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

    • 字符型: ‘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 )
  • 条件语句:

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

    所有上面算法都可以随意组合和嵌套:

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

信息表达式

我们之前的例子是这样的

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

但是有的时候我们需要在消息中增加变量,比如客人的名字怎么办?比如达到如下效果

<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>

版权说明 : 本文为转载文章, 版权为原作者所有

原文标题 : Thymeleaf教程 (四) Thymeleaf标准表达式语法(上)

原文连接 : https://blog.csdn.net/mygzs/article/details/52469770