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>
复制代码

 

posted @   hackyo  阅读(2963)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示
主题色彩