thymeleaf
<h1>thymeleaf 标准表达式语法</h1>
<h2>1 标准表达式语法</h2>
<p>它们分为四类:</p>
<ol>
<li>变量表达式,$</li>
<li>选择或星号表达式,*</li>
<li>文字国际化表达式,#</li>
<li>URL表达式,@</li>
</ol>
<h3>1.1 变量表达式</h3>
<p>变量表达式即OGNL表达式或Spring EL表达式(在Spring术语中也叫model attributes)。如下所示:
${session.user.name}</p>
<p>它们将以HTML标签的一个属性来表示:</p>
<pre><code class="language-angular2html"><span th:text="${book.author.name}" />
<li th:each="book : ${books}" /></code></pre>
<h3>1.2 选择(星号)表达式</h3>
<p>选择表达式很像变量表达式,不过它们用一个预先选择的对象来代替上下文变量容器(map)来执行,如下:
*{customer.name}</p>
<p>被指定的object由th:object属性定义:</p>
<pre><code class="language-angular2html"> <div th:object="${book}">
...
<span th:text="*{title}">...</span>
...
</div> </code></pre>
<h3>1.3 文字国际化表达式</h3>
<p>文字国际化表达式允许我们从一个外部文件获取区域文字信息(.properties),用Key索引Value,还可以提供一组参数(可选).</p>
<pre><code class="language-angular2html"> #{main.title}
#{message.entrycreated(${entryId})} </code></pre>
<p>可以在模板文件中找到这样的表达式代码:</p>
<pre><code class="language-angular2html"> <table>
...
<th th:text="#{header.address.city}">...</th>
<th th:text="#{header.address.country}">...</th>
...
</table> </code></pre>
<h3>1.4 URL表达式</h3>
<p>URL表达式指的是把一个有用的上下文或回话信息添加到URL,这个过程经常被叫做URL重写。
@{/order/list} ,
URL还可以设置参数:
@{/order/details(id=${orderId})} ,
相对路径:
@{../documents/report}</p>
<p>让我们看这些表达式:</p>
<p><code><form th:action="@{/createOrder}"><a href="main.html" th:href="@{/main}"></code></p>
<h3>1.5 变量表达式和星号表达有什么区别吗?</h3>
<p>如果不考虑上下文的情况下,两者没有区别;星号语法评估在选定对象上表达,而不是整个上下文
什么是选定对象?就是父标签的值,如下:</p>
<pre><code class="language-angular2html"><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></code></pre>
<p>这是完全等价于:</p>
<pre><code class="language-angular2html"> <div th:object="${session.user}">
<p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
</div></code></pre>
<p>当然,$和*语法也可以混合使用</p>
<pre><code class="language-angular2html"><div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div></code></pre>
<h3>1.6 表达式支持的语法</h3>
<blockquote>
<p>字面(Literals)</p>
<ul>
<li>文本文字(Text literals): 'one text', 'Another one!',…</li>
<li>数字文本(Number literals): 0, 34, 3.0, 12.3,…</li>
<li>布尔文本(Boolean literals): true, false</li>
<li>空(Null literal): null</li>
<li>文字标记(Literal tokens): one, sometext, main,…
文本操作(Text operations)</li>
<li>字符串连接(String concatenation): +</li>
<li>文本替换(Literal substitutions): |The name is ${name}|
算术运算(Arithmetic operations)</li>
<li>二元运算符(Binary operators): +, -, *, /, %</li>
<li>减号(单目运算符)Minus sign (unary operator): -
布尔操作(Boolean operations)</li>
<li>二元运算符(Binary operators):and, or</li>
<li>布尔否定(一元运算符)Boolean negation (unary operator):!, not
比较和等价(Comparisons and equality)</li>
<li>比较(Comparators): >, <, >=, <= (gt, lt, ge, le)</li>
<li>等值运算符(Equality operators):==, != (eq, ne)
条件运算符(Conditional operators)</li>
<li>If-then: (if) ? (then)</li>
<li>If-then-else: (if) ? (then) : (else)</li>
<li>Default: (value) ?: (defaultvalue)</li>
</ul>
</blockquote>
<p>所有这些特征可以被组合并嵌套:</p>
<p><code>'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))</code></p>
<h2>2. 常用的th标签</h2>
<table>
<thead>
<tr>
<th style="text-align: center;">关键字</th>
<th style="text-align: center;">功能介绍</th>
<th>案例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;">th:id</td>
<td style="text-align: center;">替换id</td>
<td><code><input th:id="'xxx' + ${collect.id}"/></code></td>
</tr>
<tr>
<td style="text-align: center;">th:text</td>
<td style="text-align: center;">文本替换</td>
<td><code><p th:text="${collect.description}">description</p></code></td>
</tr>
<tr>
<td style="text-align: center;">th:utext</td>
<td style="text-align: center;">支持html的文本替换</td>
<td><code><p th:utext="${htmlcontent}">conten</p></code></td>
</tr>
<tr>
<td style="text-align: center;">th:object</td>
<td style="text-align: center;">替换对象</td>
<td><code><div th:object="${session.user}"></code></td>
</tr>
<tr>
<td style="text-align: center;">th:value</td>
<td style="text-align: center;">属性赋值</td>
<td><code><input th:value="${user.name}" /></code></td>
</tr>
<tr>
<td style="text-align: center;">th:with</td>
<td style="text-align: center;">变量赋值运算</td>
<td><code><div th:with="isEven=${prodStat.count}%2==0"></div></code></td>
</tr>
<tr>
<td style="text-align: center;">th:style</td>
<td style="text-align: center;">设置样式</td>
<td><code>th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"</code></td>
</tr>
<tr>
<td style="text-align: center;">th:onclick</td>
<td style="text-align: center;">点击事件</td>
<td><code>th:onclick="'getCollect()'"</code></td>
</tr>
<tr>
<td style="text-align: center;">th:each</td>
<td style="text-align: center;">属性赋值</td>
<td><code>th:each="user,userStat:${users}"></code></td>
</tr>
<tr>
<td style="text-align: center;">th:if</td>
<td style="text-align: center;">判断条件</td>
<td><code><a th:if="${userId == collect.userId}" ></code></td>
</tr>
<tr>
<td style="text-align: center;">th:unless</td>
<td style="text-align: center;">和th:if判断相反</td>
<td><code><a th:href="@{/login}" th:unless=${session.user != null}>Login</a></code></td>
</tr>
<tr>
<td style="text-align: center;">th:href</td>
<td style="text-align: center;">链接地址</td>
<td><code><a th:href="@{/login}" th:unless=${session.user != null}>Login</a> /></code></td>
</tr>
<tr>
<td style="text-align: center;">th:switch</td>
<td style="text-align: center;">多路选择配合th:case 使用</td>
<td><code><div th:switch="${user.role}"></code></td>
</tr>
<tr>
<td style="text-align: center;">th:case</td>
<td style="text-align: center;">th:switch的一个分支</td>
<td><code><p th:case="'admin'">User is an administrator</p></code></td>
</tr>
<tr>
<td style="text-align: center;">th:fragment</td>
<td style="text-align: center;">布局标签,定义一个代码片段,方便其它地方引用</td>
<td><code><div th:fragment="alert"></code></td>
</tr>
<tr>
<td style="text-align: center;">th:include</td>
<td style="text-align: center;">布局标签,替换内容到引入的文件</td>
<td><code><head th:include="layout :: htmlhead" th:with="title='xx'"></head> /></code></td>
</tr>
<tr>
<td style="text-align: center;">th:replace</td>
<td style="text-align: center;">布局标签,替换整个标签到引入的文件</td>
<td><code><div th:replace="fragments/header :: title"></div></code></td>
</tr>
<tr>
<td style="text-align: center;">th:selected</td>
<td style="text-align: center;">selected选择框 选中</td>
<td><code>th:selected="(${xxx.id} == ${configObj.dd})"</code></td>
</tr>
<tr>
<td style="text-align: center;">th:src</td>
<td style="text-align: center;">图片类地址引入</td>
<td><code><img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" /></code></td>
</tr>
<tr>
<td style="text-align: center;">th:inline</td>
<td style="text-align: center;">定义js脚本可以使用变量</td>
<td><code><script type="text/javascript" th:inline="javascript"></code></td>
</tr>
<tr>
<td style="text-align: center;">th:action</td>
<td style="text-align: center;">表单提交的地址</td>
<td><code><form action="subscribe.html" th:action="@{/subscribe}"></code></td>
</tr>
<tr>
<td style="text-align: center;">th:remove</td>
<td style="text-align: center;">删除某个属性</td>
<td><code><tr th:remove="all"></code> 1.all:删除包含标签和所有的孩子。</td>
</tr>
<tr>
<td style="text-align: center;">th:attr</td>
<td style="text-align: center;">设置标签属性,多个属性可以用逗号分隔</td>
<td>比如 <code>th:attr="src=@{/image/aa.jpg},title=#{logo}"</code>,此标签不太优雅,一般用的比较少。</td>
</tr>
</tbody>
</table>
<h2>3. 几种常用的使用方法</h2>
<h3>3.1 赋值、字符串拼接</h3>
<pre><code class="language-html"><p th:text="${collect.description}">description</p>
<span th:text="'Welcome to our application, ' + ${user.name} + '!'"></code></pre>
<p>字符串拼接还有另外一种简洁的写法
<code><span th:text="|Welcome to our application, ${user.name}!|"></code></p>
<h3>3.2 条件判断 If/Unless</h3>
<p>Thymeleaf中使用th:if和th:unless属性进行条件判断,下面的例子中,<a>标签只有在th:if中条件成立时才显示:</p>
<p><code><a th:if="${myself=='yes'}" > </i> </a><a th:unless=${session.user != null} th:href="@{/login}" >Login</a></code></p>
<p>th:unless于th:if恰好相反,只有表达式中的条件不成立,才会显示其内容。也可以使用 (if) ? (then) : (else) 这种语法来判断显示的内容</p>
<h3>3.3 for 循环</h3>
<pre><code class="language-html"> <tr th:each="collect,iterStat : ${collects}">
<th scope="row" th:text="${collect.id}">1</th>
<td ><img th:src="${collect.webLogo}"/></td>
<td th:text="${collect.url}">Mark</td>
<td th:text="${collect.title}">Otto</td>
<td th:text="${collect.description}">@mdo</td>
<td th:text="${terStat.index}">index</td>
</tr></code></pre>
<p>iterStat称作状态变量,属性有:</p>
<ul>
<li>index:当前迭代对象的index(从0开始计算)</li>
<li>count: 当前迭代对象的index(从1开始计算)</li>
<li>size:被迭代对象的大小</li>
<li>current:当前迭代变量</li>
<li>even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)</li>
<li>first:布尔值,当前循环是否是第一个</li>
<li>last:布尔值,当前循环是否是最后一个
<h3>3.4 URL</h3></li>
</ul>
<p>URL在Web应用模板中占据着十分重要的地位,需要特别注意的是Thymeleaf对于URL的处理是通过语法@{…}来处理的。
如果需要Thymeleaf对URL进行渲染,那么务必使用th:href,th:src等属性,下面是一个例子</p>
<pre><code class="language-html"><!-- Will produce 'http://localhost:8080/standard/unread' (plus rewriting) -->
<a th:href="@{/standard/{type}(type=${type})}">view</a>
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a></code></pre>
<p>设置背景</p>
<p><code><div th:style="'background:url(' + @{/<path-to-image>} + ');'"></div></code></p>
<p>根据属性值改变背景
<code><div class="media-object resource-card-image" th:style="'background:url(' + @{(${collect.webLogo}=='' ? 'img/favicon.png' : ${collect.webLogo})} + ')'" ></div></code></p>
<p>几点说明:</p>
<ul>
<li>上例中URL最后的(orderId=${o.id}) 表示将括号内的内容作为URL参数处理,该语法避免使用字符串拼接,大大提高了可读性</li>
<li>@{...}表达式中可以通过{orderId}访问Context中的orderId变量</li>
<li>@{/order}是Context相关的相对路径,在渲染时会自动添加上当前Web应用的Context名字,假设context名字为app,那么结果应该是/app/order
<h3>3.5 内联js</h3>
<p>内联文本:[[…]]内联文本的表示方式,使用时,必须先用th:inline=”text/javascript/none”激活,th:inline可以在父级标签内使用,
甚至作为body的标签。内联文本尽管比th:text的代码少,不利于原型显示。</p>
<pre><code><script th:inline="javascript">
/*<![CDATA[*/
...
var username = /*[[${sesion.user.name}]]*/ 'Sebastian';
var size = /*[[${size}]]*/ 0;
...
/*]]>*/
</script></code></pre>
<p>js附加代码</p>
<pre><code class="language-html">/*[+
var msg = 'This is a working application';
+]*/</code></pre>
<p>js移除代码:</p>
<pre><code class="language-html">/*[- */
var msg = 'This is a non-working template';
/* -]*/</code></pre>
<h3>3.6 内嵌变量</h3>
<p>为了模板更加易用,Thymeleaf还提供了一系列Utility对象(内置于Context中),可以通过#直接访问:</p></li>
<li>dates : java.util.Date的功能方法类。</li>
<li>calendars : 类似#dates,面向java.util.Calendar</li>
<li>numbers : 格式化数字的功能方法类</li>
<li>strings : 字符串对象的功能类,contains,startWiths,prepending/appending等等。</li>
<li>objects: 对objects的功能类操作。</li>
<li>bools: 对布尔值求值的功能方法。</li>
<li>arrays:对数组的功能类方法。</li>
<li>lists: 对lists功能类方法</li>
<li>sets</li>
<li>maps
<h2>4 使用thymeleaf布局</h2>
<p>定义代码片段</p>
<pre><code class="language-html"><footer th:fragment="copy">
© 2016
</footer></code></pre>
<p>在页面任何地方引入:</p>
<pre><code class="language-html"><body><div th:include="footer :: copy"></div><div th:replace="footer :: copy"></div></body></code></pre>
<p>th:include 和 th:replace区别,include只是加载,replace是替换,返回的HTML如下:</p>
<pre><code class="language-html"><body><div> © 2016 </div><footer>© 2016 </footer></body></code></pre>
<p>下面是一个常用的后台页面布局,将整个页面分为头部,尾部、菜单栏、隐藏栏,点击菜单只改变content区域的页面</p>
<pre><code class="language-html"><body class="layout-fixed">
<div th:fragment="navbar" class="wrapper" role="navigation">
<div th:replace="fragments/header :: header">Header</div>
<div th:replace="fragments/left :: left">left</div>
<div th:replace="fragments/sidebar :: sidebar">sidebar</div>
<div layout:fragment="content" id="content" ></div>
<div th:replace="fragments/footer :: footer">footer</div>
</div>
</body></code></pre>
<p>任何页面想使用这样的布局值只需要替换中见的 content模块即可</p>
<pre><code class="language-html"><html xmlns:th="http://www.thymeleaf.org" layout:decorator="layout">
<body>
<section layout:fragment="content">
...</code></pre>
<p>也可以在引用模版的时候传参</p>
<pre><code class="language-html"><head th:include="layout :: htmlhead" th:with="title='Hello'"></head></code></pre>
<p>layout 是文件地址,如果有文件夹可以这样写 fileName/layout:htmlhead
htmlhead 是指定义的代码片段 如 th:fragment="copy"</p>
<h4></h4></li>
</ul>