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