天天看点

用Thymeleaf替换JSP

Thymeleaf是一个Java库。它是XML、XHTML、HTML5等格式的模板引擎,可以用于Web项目和非Web项目。Thymeleaf很适合作为Web应用的视图的业务逻辑层,还可以在离线环境下处理XML文件。

用Thymeleaf替换JSP

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>

  /tbody>

/table>

今天(2013.11.4日),Thymeleaf发布了最新的2.1.0版。