thymeleaf

Thymeleaf是什么

Thymeleaf是⾯向Web和独⽴环境的现代服务器端Java模板引擎, 可以处理HTML, XML, JavaScript, CSS甚⾄纯⽂本。 

html

<html xmlns:th="http://www.thymeleaf.org"> java

获取属性 

$ {x}将返回存储在Thymeleaf上下⽂中的变量x或做为请求属性。
$ {param.x}将返回⼀个名为x的请求参数(多是多值的) 。
$ {session.x}将返回⼀个名为x的会话属性。
$ {application.x}将返回⼀个名为xservlet上下⽂属性。 
spring

标准表达式

简单表达式

变量表达式$ {...}
选择变量表达式\* {...}
消息表达式: #{...}
连接⽹址表达式@ {...}
⽚段表达式: 〜{...}

文字

⽂字'one text' , 'Another one!'
数字字⾯值0,34,3.0,12.3...
布尔⽂字truefalse
空字⾯值null
⽂字Tokenonesometextmain...
数组

文本操做:

字符串链接+
⽂本替换|The name is ${name}|
服务器

算术运算符

⼆进制运算符+-\*/%
负号(⼀元运算符)-
session

布尔运算符

⼆进制运算符and or
布尔否认(⼀元运算符) : ! , not
app

比较和相等运算符:

⽐较运算符><> =<=gtltgele
相等运算符==, ! =eqne
less

条件运算符

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

特殊符号:

哑操做符: \_spa

 

全部这些功能能够组合和嵌套:

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

th:text  

<p th:text="#{home.welcome}">Welcome to our grocery store!</p> 

spring: messages: basename: i18n
/hello

th:utext

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>

⼯具表达式对象

#execInfo: 有关正在处理的模板的信息。
#messages: ⽤于在变量表达式中获取外部化消息的⽅法, 与使⽤#{...}语法得到的⽅式相同。
#uris: 转义URL / URI部分的⽅法
#conversions: 执⾏配置的转换服务(若是有的话) 的⽅法。
#datesjava.util.Date对象的⽅法: 格式化, 组件提取等
#calendars: 相似于#dates, 但对于java.util.Calendar对象。
#numbers: ⽤于格式化数字对象的⽅法。
#stringsString对象的⽅法: containsstartsWithprepending /appending
#objects: ⼀般对象的⽅法。
#bools: 布尔评估的⽅法。
#arrays: 数组的⽅法。
#lists: 列表的⽅法。
#sets: 集合的⽅法。
#maps: 地图⽅法。
#aggregates: 在数组或集合上建立聚合的⽅法。
#ids: 处理可能重复的id属性的⽅法(例如, 做为迭代的结果) 。

星号语法

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

URL连接

<!-- Will produce 'http://localhost:8080/gtvg/order/detail
s?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>

⽂本替换

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

哑操做符号

<span th:text="${user.name} ?: _">no user authenticated</span>

if/unless

<a th:href="@{/login}" th:unless=${session.user != null}>Login</a>

switch

 
<div th:switch="${user.role}">
  <p th:case="'admin'">User is an administrator</p>
  <p th:case="#{roles.manager}">User is a manager</p>
  <p th:case="*">User is some other thing</p>
</div>
 

th:each

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
    <!-- 不存在则忽略,显示hello null!(能够经过默认值进行设置)-->
    <p th:text="'Hello ' + (${name}?:'admin')">3333</p>
    <table>
        <tr>
            <th>ID</th>
            <th>NAME</th>
            <th>AGE</th>
        </tr>
        <tr th:each="emp : ${empList}">
            <td th:text="${emp.id}">1</td>
            <td th:text="${emp.name}"></td>
            <td th:text="${emp.age}">18</td>
        </tr>
    </table>
</body>
</html>

经常使用标签

相关文章
相关标签/搜索