freemarker和thymeleaf的使用樣例


最近需要對公司項目首頁使用Java模板重做,以提高首屏加載速度和優化SEO。

在選擇模板時發現freemarker和thymeleaf最為常用。

兩者最大的區別在於語法,對性能方面未作測試,具體性能測試可參考https://www.ktanx.com/blog/p/4965

本篇博客主要研究兩者語法上的不同。

代碼已上傳至Github:https://github.com/hackyoMa/template

分別演示兩者的引用文件、循環、判斷和輸出。

freemarker:

<#include "layout/header.ftl">
<!DOCTYPE html>
<html lang="cmn-Hans">
<@header title="freemarker"></@header>
<body>
<#list userList as username>
<#if username == "王五">
    <p>下面是王五</p>
</#if>
<p>${username}</p>
</#list>
</body>
</html>

thymeleaf:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="cmn-Hans">
<head th:include="/layout/header :: header" th:with="title='Thymeleaf'">
</head>
<body>
<div th:each="name:${userList}">
    <p th:if="${name}=='王五'">下面是王五</p>
    <p th:text="${name}">姓名</p>
</div>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM