您的位置:首页 > 编程语言 > Java开发

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

2016-09-08 15:31 253 查看

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>


我来解释下:

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,那么添加一个小菜单在我们的网站吧

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

基本类型操作

字符型

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

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


数值型

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

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

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


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

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


双下划线可以用\_\_转义哦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  html web java thymeleaf