首页 > 技术文章 > thymeleaf中的Literals

suncj 2014-10-16 17:29 原文

Literals即为文字

  一.Text literals:文本文字

  文本文字只是字符串指定的单引号之间。他们可以包含任何字符,但你应避免任何单引号里面\ '

<p>
  Now you are looking at a <span th:text="'working web application'">template file</span>.
</p>

如果是带空格什么的不连贯的一段话,例如working web application,必须要用单引号引起来才能被解析,如果是如下则不能被解析,会出现错误:

<p>
  Now you are looking at a <span th:text="working web application">template file</span>.
</p>

如果是连贯的不分开的如下是正确被解析的:

<p>
  Now you are looking at a <span th:text="workingwebapplication">template file</span>.
</p>

二:文字数量

  数字文字看起来像:数字。

  例子:

<p>The year is <span th:text="2014">1492</span>.</p>
<p>In two years, it will be <span th:text="2013 + 2">1494</span>.</p>

显示结果:

The year is 2014
In two years, it will be 2016

 三.布尔型常量

  布尔常量true和false。例如:

<div th:if="${user.isAdmin()} == false"> 如果是false我显示</div>

注意,在上面的例子中,==false写在括号外,因此Thymeleaf本身照顾它。如果它是写在括号内,这将是OGNL/SpringEL引擎展示出:

<div th:if="${user.isAdmin() == false}"> 如果是false我显示</div>

 

四.空的文字

<div th:if="${variable.something} == null">如果为空我显示</div>

五.附加的文本

  文本,无论他们是文字或评估的结果变量或消息表达式,可以很容易地添加使用+运算符:

th:text="'The name of the user is ' + ${user.name}"

六.文字替换:

  文字替换允许简单的格式化字符串包含值的变量而不需要附加的文字……‘+’……”。这些替换必须包围竖线(|),如:

<span th:text="|Welcome to our application, ${user.name}!|">

等同于:

<span th:text="'Welcome to our application,'+ ${user.name}+'!'">

文字替换可以结合其他类型的表达式:

<span th:text="${onevar} + ' ' + |${twovar}, ${threevar}|">

算术运算:

<p th:with="isEven=(${user.count} % 2 == 0)">显示</p>

 

推荐阅读