Thymeleaf教程 (五) Thymeleaf标准表达式语法(下)

URL连接

URL连接有如下几种类型:   html

  • 绝对地址,如http://www.thymeleaf.org
  • 相对地址 
    • 相对页面地址.如:/user/login.html
    • 服务器相对地址如:~/billing/processInvoice(部署在同服务器,不一样域名的地址)

让咱们来使用th:href属性:web

<!-- Will produce 'http://localhost:8080/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html"
th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>

我来解释下:express

  • th:href属性修饰符:它将计算并替换使用href连接URL 值,并放入的href属性中。
  • 咱们可使用URL参数的表达式(好比在orderId=${o.id} )
  • 若是须要多个参数,这些将由逗号分隔,好比:@{/order/process(execId=${execId},execType=’FAST’)}
  • 变量也容许URL路径中使用,好比:@{/order/{orderId}/details(orderId=${orderId})}
  • URL中以”/“开头的路径(好比/order/details)将会加上服务器地址和域名。造成完整的URL
  • th:href中能够直接使用静态地址。

URL能够用复杂的表达式:服务器

<a th:href="@{${url}(orderId=${o.id})}">view</a>
<a th:href="@{'/details/'+${user.login}(orderId=${o.id})}">view</a>

如今咱们知道如何建立连接的url,那么添加一个小菜单在咱们的网站吧session

<p>Please select an option</p>
<ol>
<li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li>
<li><a href="order/list.html" th:href="@{/order/list}">Order List</a></li>
<li><a href="subscribe.html" th:href="@{/subscribe}">Subscribe to our Newsletter</a></li>
<li><a href="userprofile.html" th:href="@{/userprofile}">See User Profile</a></li>
</ol>

针对同服务器地址,不一样域名的URL。能够这样写@{~/path/to/something}app

基本类型操做

字符型

文本文字能够用单引号来包含。须要转义的话能够用\’转义网站

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

数值型

数值型操做简单。以下所示:url

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

Boolean型

boolean型不是true就是false:spa

<div th:if="${user.isAdmin()} == false"> ...

注意,在上面的例子中,= = false写在括号外,所以是Thymeleaf自己负责解析解析它。若是是写在括号内,它将由OGNL负责解析:code

<div th:if="${user.isAdmin() == false}"> ...

Null型

<div th:if="${variable.something} == null"> ...

Literal tokens(不明白什么意思,大概是字符串文本)

Numeric, boolean 和 null都是字符串文本的一种类型。

只是使表达式更加简洁。工做时终将解析成字符串文本(‘。。。。。。’),可是他们有更多的限制,好比只能用数字(0~9),下划线,.,没有空格,没有逗号等等。

因此当咱们仅仅用字符串的话,能够用这种:

<div th:class="content">...</div>

替换掉

<div th:class="'content'">...</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}|">

注意:${…}表达式能够被放在|….|之间,可是不能放在’….’之间哦

算术运算

也能够用一些算术运算符:+ , - , * , / , % .

th:with="isEven=(${prodStat.count} % 2 == 0)"

比较与相等

 > , < , >= ,<=,== 和 !=均可以用,可是<,>这两个在必须转义。

th:if="${prodStat.count} &gt; 1"
th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')"

固然嫌转义什么的太麻烦小朋友,能够用别名替代 gt ( > ), lt ( < ), ge ( >= ), le ( <= ), not ( ! ), eq ( == ),neq / ne ( != ).

条件表达式

能够这样用

<tr th:class="${row.even}? 'even' : 'odd'">
...
</tr>

也能够这样中

<tr th:class="${row.even}? (${row.first}? 'first' : 'even') : 'odd'">
...
</tr>

能够省略false的返回值,固然若是false那么返回的是一个空值

<tr th:class="${row.even}? (${row.first}? 'first' : 'even') : 'odd'">
...
</tr>

默认表达式

默认表达式能够简化表达式,我的不建议用,阅读性差。如:

<div th:object="${session.user}">
...
<p>Age: <span th:text="*{age}?: '(no age specified)'">27</span>.</p>
</div>

解释一下:age若是是null的话就执行’(no age specified)’这段,不然就显示age。跟如下同样:

<p>Age: <span th:text="*{age != null}? *{age} : '(no age specified)'">27</span>.</p>

还能够嵌套玩:

<p>
Name:
<span th:text="*{firstName}?: (*{admin}? 'Admin' : #{default.username})">Sebastian</span>
</p>

预处理表达式

有的时候咱们须要预处理一些信息到表达式中。好比某个变量的名字是变的,怎么办?预处理来了。

预处理表达式用 __${expression}__ 双下划线包裹,举个栗子: 
咱们在外部资源文件中配了这个属性:

article.text=@myapp.translator.Translator@translateToSpanish({0})

咱们能够在模板中表达式是这样子的:

<p th:text="${__#{article.text('textVar')}__}">Some text here...</p>

那么引擎会首先从资源文件中获取article.text的值,再执行它。

<p th:text="${@myapp.translator.Translator@translateToFrench(textVar)}">Some text here...</p>

双下划线能够用\_\_转义哦!

相关文章
相关标签/搜索