天天看点

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