天天看點

用Thymeleaf替換JSP用Thymeleaf替換JSP

版權聲明:本文為部落客chszs的原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/chszs/article/details/14140215

用Thymeleaf替換JSP

作者:chszs,轉載需注明。部落格首頁:

http://blog.csdn.net/chszs

Thymeleaf是一個Java庫。它是XML、XHTML、HTML5等格式的模闆引擎,可以用于Web項目和非Web項目。Thymeleaf很适合作為Web應用的視圖的業務邏輯層,還可以在離線環境下處理XML檔案。

Thymeleaf可以作為Spring MVC的可選子產品,也可以直接作為JSP的替代。Thymeleaf提供了兩個版本,分别是Standard方言與SpringStandard(Spring MVC 3)方言,并且可以通過建立自定義方言進行擴充。

Thymeleaf的主要目标在于提供一種可被浏覽器正确顯示的、格式良好的模闆建立方式,是以也可以用作靜态模組化。

Thymelea模闆引擎與Velocity、FreeMarker相似。

代碼例子:

<table>
  <thead>
    <tr>
      <th th:text="#{msgs.headers.name}">Name</th>
      <th th:text="#{msgs.headers.price}">Price</th>
    </tr>
  </thead>
  <tbody>
    <tr th:each="prod : ${allProducts}">
      <td th:text="${prod.name}">Oranges</td>
      <td th:text="${#numbers.formatDecimal(prod.price,1,2)}">0.99</td>
    </tr>
  </tbody>
</table>           

今天(2013.11.4日),Thymeleaf釋出了最新的2.1.0版。

下載下傳位址:

https://sourceforge.net/projects/thymeleaf/files/thymeleaf/2.1.0.RELEASE/