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

来源:互联网 发布:mac版pscc2017破解补丁 编辑:程序博客网 时间:2024/05/22 03:18

URL链接

URL链接有以下几种类型:   

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

让我们来使用th:href属性:

<!-- 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

我来解释下:

  • 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>
  • 1
  • 2

现在我们知道如何创建链接的url,那么添加一个小菜单在我们的网站吧

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

基本类型操作

字符型

文本文字可以用单引号来包含。需要转义的话可以用\’转义

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

数值型

数值型操作简单。如下所示:

<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>
  • 1
  • 2

Boolean型

boolean型不是true就是false:

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

注意,在上面的例子中,= = false写在括号外,因此是Thymeleaf本身负责解析解析它。如果是写在括号内,它将由OGNL负责解析:

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

Null型

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

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

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

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

所以当我们仅仅用字符串的话,可以用这种:

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

替换掉

<div th:class="'content'">...</div>
  • 1

文本间连接

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

高级文本连接用法

我们可以用“|”包含住想要连接的文本,替换’…’ + ‘…’方式,这样就可以省心不少。

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

替换原来的

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

高级点可以这样

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

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

算术运算

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

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

比较与相等

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

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

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

条件表达式

可以这样用

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

也可以这样中

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

可以省略false的返回值,当然如果false那么返回的是一个空值

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

默认表达式

默认表达式可以简化表达式,个人不建议用,阅读性差。如:

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

解释一下:age如果是null的话就执行’(no age specified)’这段,否则就显示age。跟以下一样:

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

还可以嵌套玩:

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

预处理表达式

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

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

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

我们可以在模板中表达式是这样子的:

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

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

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

双下划线可以用\_\_转义哦!

阅读全文
0 0