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