Thymeleaf 基本用法總結

<div id="cnblogs_post_body" class="blogpost-body"><p>&nbsp;1、引用命名空間&nbsp;&lt;html xmlns:th="http://www.thymeleaf.org"&gt;&nbsp;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 在html中引入此命名空間,可避免編輯器出現html驗證錯誤,雖然加不加命名空間對Thymeleaf的功能沒有任何影響。</p> <p>&nbsp;</p> <p>2、輸出內容</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 2.1 &nbsp;&lt;p th:text="#{home.welcome}"&gt;Welcome to our grocery store!&lt;/p&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 說明:</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1. th:text &nbsp;用來將內容輸出到所在標籤的body中。</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.&nbsp;#{home.welcome} 用來引入數據home對象中的 welcome屬性。</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3. 能夠用th:utext 用來顯示「unescaped 」 的html內容。</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 2.2 &nbsp;&nbsp;&nbsp;&lt;p&gt;Today is: &lt;span th:text="${today}"&gt;13 February 2011&lt;/span&gt;&lt;/p&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 說明:${today} 用來引用 today 變量</p> <p>3、訪問對象&nbsp; &nbsp; &nbsp;&nbsp;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;${param.x} 返回名爲x 的 request參數。(可能有多個值)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;${session.x} 返回名爲x的Session參數。</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;${application.x} 返回名爲 servlet context 的參數。</p> <p>&nbsp; &nbsp; &nbsp;</p> <p>4、基本語法</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;4.1 &nbsp;#{home.welcome} -- &nbsp;訪問數據</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;4.2 &nbsp;#{home.welcome(${session.user.name})} &nbsp;-- 格式化數據 當 home.welcome 爲 "abcdegf{0}" &nbsp;相似這種內容時。(多個參數以逗句分隔)。</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;4.3 &nbsp;${today} --- 訪問變量</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;4.4 &nbsp;訪問基本對象</p> <p style="margin-left: 30px">#ctx: the context object.<br>#vars: the context variables.<br>#locale: the context locale.<br>#request: (only in Web Contexts) the HttpServletRequest object.<br>#response: (only in Web Contexts) the HttpServletResponse object.<br>#session: (only in Web Contexts) the HttpSession object.<br>#servletContext: (only in Web Contexts) the ServletContext object.</p> <p style="margin-left: 30px">其它公共對象參考:&nbsp;http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-a-expression-basic-objects</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 4.5 &nbsp;日期的輸出</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&lt;span th:text="${#calendars.format(today,'dd MMMM yyyy')}"&gt;13 May 2011&lt;/span&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 4.6 &nbsp;星號語法</p> <p style="margin-left: 30px"> &lt;div th:object="${session.user}"&gt;<br> &lt;p&gt;Name: &lt;span th:text="*{firstName}"&gt;Sebastian&lt;/span&gt;.&lt;/p&gt;<br> &lt;p&gt;Surname: &lt;span th:text="*{lastName}"&gt;Pepper&lt;/span&gt;.&lt;/p&gt;<br> &lt;p&gt;Nationality: &lt;span th:text="*{nationality}"&gt;Saturn&lt;/span&gt;.&lt;/p&gt;<br> &lt;/div&gt;</p> <p style="margin-left: 30px">4.7 &nbsp;輸出URL</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;a href="product/list.html" th:href="@{/product/list}"&gt;Product List&lt;/a&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}"&gt;view&lt;/a&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;4.8 &nbsp;使用代碼段</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;div th:insert="~{commons :: main}"&gt;...&lt;/div&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;4.9 &nbsp;直接輸出內容&nbsp; &nbsp;</p> <p style="margin-left: 30px">&lt;span th:text="'working web application'"&gt; -- 輸出字符</p> <p style="margin-left: 30px">&lt;span th:text="2013 + 2"&gt; &nbsp;-- 輸出數據表達式</p> <p style="margin-left: 30px">&lt;div th:if="${user.isAdmin()} == false"&gt; &nbsp;--輸出布爾表達式</p> <p style="margin-left: 30px">&lt;span th:text="'Welcome to our application, ' + ${user.name} + '!'"&gt; -- 帶變量的</p> <p style="margin-left: 30px">4.10 條件表達式</p> <p style="margin-left: 30px">&lt;tr th:class="${row.even}? 'even' : 'odd'"&gt;<br> ... &nbsp;<br>&lt;/tr&gt;</p> <p style="margin-left: 30px">&lt;tr th:class="${row.even}? 'alt'"&gt;<br> ...省略 false 結果的表達方式<br>&lt;/tr&gt;</p> <p style="margin-left: 30px">&lt;div th:object="${session.user}"&gt;<br> ...省略 true 結果的表達方式<br> &lt;p&gt;Age: &lt;span th:text="*{age}?: '(no age specified)'"&gt;27&lt;/span&gt;.&lt;/p&gt;<br>&lt;/div&gt;</p> <p style="margin-left: 30px">&lt;span th:text="${user.name} ?: _"&gt;no user authenticated&lt;/span&gt; --不作任何處理時用下劃線 _ 表示</p> <p style="margin-left: 30px">4.11 &nbsp;格式化&nbsp;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;td th:text="${{user.lastAccessDate}}"&gt;...&lt;/td&gt; --${{.}} &nbsp;調用默認的格式化器來輸出結果。</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;4.12 &nbsp;預處理</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;p th:text="${__#{article.text('textVar')}__}"&gt;Some text here...&lt;/p&gt; &nbsp;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;說明:thymeleaf 的處理模板內容的順序與書寫順序無關,只能經過 &nbsp;__${expression}__ ,來將須要先一步計算出來後面 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;要用的變量指定爲優化處理。</p> <p>&nbsp;</p> <p>&nbsp;5、設置 Attribute 值</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;5.1 設置任何Attribute 的方法</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/&gt; &nbsp; --設置單個</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;img src="../../images/gtvglogo.png" &nbsp;th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" /&gt; &nbsp;--一次設置多個</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 5.2 設置一些內置的Attribute的方法&nbsp; &nbsp;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;li&gt;&lt;a href="product/list.html" th:href="@{/product/list}"&gt;Product List&lt;/a&gt;&lt;/li&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;form action="subscribe.html" th:action="@{/subscribe}"&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;img src="../../images/gtvglogo.png" &nbsp;th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" /&gt; -- 一次設置多個(alt title)的方法</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;其它的可用屬性:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 5.3 設置html裏沒有指的任何屬性的語法</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&lt;span th:whatever="${user.name}"&gt;...&lt;/span&gt; &nbsp; ---whatever 能夠換成任何你想設的屬性</p> <p>&nbsp;</p> <p>6、循環輸出的語法</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;6.1 基本循環</p> <p style="margin-left: 30px">&lt;tr th:each="prod : ${prods}"&gt;</p> <p style="margin-left: 30px">&nbsp; &nbsp; &nbsp;&lt;td th:text="${prod.name}"&gt;Onions&lt;/td&gt;<br>&nbsp; &nbsp; &nbsp;&lt;td th:text="${prod.price}"&gt;2.41&lt;/td&gt;<br>&nbsp; &nbsp; &nbsp;&lt;td th:text="${prod.inStock}? #{true} : #{false}"&gt;yes&lt;/td&gt;<br> &lt;/tr&gt;</p> <p style="margin-left: 30px">6.2 循環狀態的使用</p> <p style="margin-left: 30px">&lt;table&gt;<br> &lt;tr&gt;<br> &lt;th&gt;NAME&lt;/th&gt;<br> &lt;th&gt;PRICE&lt;/th&gt;<br> &lt;th&gt;IN STOCK&lt;/th&gt;<br> &lt;/tr&gt;<br> &lt;tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'"&gt;<br> &lt;td th:text="${prod.name}"&gt;Onions&lt;/td&gt;<br> &lt;td th:text="${prod.price}"&gt;2.41&lt;/td&gt;<br> &lt;td th:text="${prod.inStock}? #{true} : #{false}"&gt;yes&lt;/td&gt;<br> &lt;/tr&gt;<br>&lt;/table&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;關於狀態的其它信息的使用詳細參考:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#keeping-iteration-status</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;</p> <p>7、條件判斷</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;7.1 if 和 unless</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}"&gt;view&lt;/a&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&lt;a href="comments.html" &nbsp;th:href="@{/product/comments(prodId=${prod.id})}" &nbsp; th:if="${not #lists.isEmpty(prod.comments)}"&gt;view&lt;/a&gt;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;7.2 switch 語句</p> <p style="margin-left: 30px">&lt;div th:switch="${user.role}"&gt;<br> &lt;p th:case="'admin'"&gt;User is an administrator&lt;/p&gt;<br> &lt;p th:case="#{roles.manager}"&gt;User is a manager&lt;/p&gt;<br> &lt;p th:case="*"&gt;User is some other thing&lt;/p&gt; &nbsp; &nbsp;--默認的 case 至關於default<br>&lt;/div&gt;</p> <p>&nbsp;</p> <p>8、模板 include</p> <p>&nbsp; &nbsp; &nbsp; 8.1 定義和引用代碼塊</p> <p>&nbsp; &nbsp; &nbsp; 定義代碼塊</p> <p style="margin-left: 30px">&lt;!DOCTYPE html&gt;</p> <p style="margin-left: 30px">&lt;html xmlns:th="http://www.thymeleaf.org"&gt;</p> <p style="margin-left: 30px"> &lt;body&gt;<br> <br> &lt;div th:fragment="copy"&gt;<br> &amp;copy; 2011 The Good Thymes Virtual Grocery<br> &lt;/div&gt;<br> <br> &lt;/body&gt;<br> <br>&lt;/html&gt;</p> <p style="margin-left: 30px">引用代碼塊</p> <p style="margin-left: 30px">&lt;body&gt;</p> <p style="margin-left: 30px"> ...</p> <p style="margin-left: 30px"> &lt;div th:insert="~{footer :: copy}"&gt;&lt;/div&gt;<br> <br>&lt;/body&gt;</p> <p style="margin-left: 30px">引用未用fragment 標註的代碼塊&nbsp;</p> <p style="margin-left: 30px">&lt;div id="copy-section"&gt;<br> &amp;copy; 2011 The Good Thymes Virtual Grocery<br>&lt;/div&gt;</p> <p style="margin-left: 30px">&lt;body&gt;</p> <p style="margin-left: 30px"> ...</p> <p style="margin-left: 30px"> &lt;div th:insert="~{footer :: #copy-section}"&gt;&lt;/div&gt;<br> <br>&lt;/body&gt;</p> <p style="margin-left: 30px">8.2&nbsp;th:insert th:replace th:include 之間的區別</p> <p style="margin-left: 30px">th:insert &nbsp;--- 插入代碼塊 &nbsp; &nbsp;th:replace -- 替換代碼塊會替換掉容器標籤 &nbsp; th:include ---- 和insert類似但只會插入fragment標註body內的內容。</p> <p style="margin-left: 30px">8.3 &nbsp;帶參數的代碼段</p> <p style="margin-left: 30px">&lt;div th:fragment="frag (onevar,twovar)"&gt;<br> &lt;p th:text="${onevar} + ' - ' + ${twovar}"&gt;...&lt;/p&gt;<br>&lt;/div&gt;</p> <p>&nbsp; &nbsp; &nbsp;&lt;div th:replace="::frag (${value1},${value2})"&gt;...&lt;/div&gt;<br>&nbsp; &nbsp; &nbsp;&lt;div th:replace="::frag (onevar=${value1},twovar=${value2})"&gt;...&lt;/div&gt;</p> <p>&nbsp;</p> <p>9、局部變量的使用示例</p> <p style="margin-left: 30px">&lt;div th:with="firstPer=${persons[0]}"&gt;<br> &lt;p&gt;<br> The name of the first person is &lt;span th:text="${firstPer.name}"&gt;Julius Caesar&lt;/span&gt;.<br> &lt;/p&gt;<br>&lt;/div&gt;</p> <p style="margin-left: 30px">&lt;div th:with="firstPer=${persons[0]},secondPer=${persons[1]}"&gt;<br> &lt;p&gt;<br> The name of the first person is &lt;span th:text="${firstPer.name}"&gt;Julius Caesar&lt;/span&gt;.<br> &lt;/p&gt;<br> &lt;p&gt;<br> But the name of the second person is <br> &lt;span th:text="${secondPer.name}"&gt;Marcus Antonius&lt;/span&gt;.<br> &lt;/p&gt;<br>&lt;/div&gt;</p> <p>10、註釋</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<code>&lt;!-- ... --&gt; &nbsp;</code></p> <p>11、說明</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 以上只列出Thymeleaf了簡要經常使用的語法和使用方式,更多詳情的說明和規則請參見:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf</p></div>html

相關文章
相關標籤/搜索