当前位置: 首页 > news >正文

Freemarker

Freemarker简介

Freemarker是一个用Java语言编写的模板引擎,用于基于模板和数据生成文本输出。它可以用于生成HTML网页、XML文档、电子邮件、配置文件等任何格式的文本。Freemarker将业务逻辑与表示逻辑分离,使得开发人员可以专注于功能实现,而设计师可以专注于页面布局。

快速入门

1. 添加依赖

如果你使用的是Maven项目,可以在pom.xml中添加如下依赖:

<dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.31</version>
</dependency>
2. 配置环境

创建一个Configuration对象,指定模板加载路径。

import freemarker.template.Configuration;
import freemarker.template.Template;Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
cfg.setDirectoryForTemplateLoading(new File("path/to/your/templates/directory"));
3. 创建模型

模型是传递给模板的数据。

Map<String, Object> model = new HashMap<>();
model.put("name", "John Doe");
model.put("age", 30);
4. 加载并合并模板
Template temp = cfg.getTemplate("templateName.ftl");
Writer out = new PrintWriter(new FileOutputStream("output.html"), true);
temp.process(model, out);

案例一

假设你有一个简单的HTML模板helloWorld.ftl

<!DOCTYPE html>
<html>
<head><title>Hello World</title>
</head>
<body><h1>Welcome, ${name}!</h1><p>You are ${age} years old.</p>
</body>
</html>

你可以使用以下Java代码生成HTML文件:

import java.io.*;
import java.util.Map;
import java.util.HashMap;
import freemarker.template.*;public class HelloWorld {public static void main(String[] args) throws Exception {Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);cfg.setDirectoryForTemplateLoading(new File("templates"));Map<String, Object> model = new HashMap<>();model.put("name", "John Doe");model.put("age", 30);Template temp = cfg.getTemplate("helloWorld.ftl");Writer out = new PrintWriter(new FileOutputStream("output.html"), true);temp.process(model, out);}
}

案例二

更复杂的案例可能涉及模板继承、列表循环、条件判断等。例如,你可能有如下的模板结构:

  • base.ftl: 基础模板,包含头部和尾部。
  • index.ftl: 继承base.ftl,添加动态内容。
base.ftl
<!DOCTYPE html>
<html>
<head><title>${title}</title>
</head>
<body><header><h1>Welcome to our site</h1></header><div id="content"><#include "content.ftl"></div><footer><p>&copy; 2024 Our Company</p></footer>
</body>
</html>
index.ftl
<@base title="Home Page"><#list items as item><div><h2>${item.title}</h2><p>${item.description}</p></div></#list>
</@base>

在这个例子中,base.ftl是一个基础模板,index.ftl通过@base指令继承了基础模板,并传入了标题参数。index.ftl还包含了对items列表的循环。

这个案例展示了Freemarker的模板继承和列表处理能力,适用于构建复杂且可重用的页面结构。

http://www.lryc.cn/news/366530.html

相关文章:

  • 基于Zero-shot实现LLM信息抽取
  • 【python】tkinter GUI编程经典用法,Label标签组件应用实战详解
  • 国产操作系统上给麒麟虚拟机安装virtualbox增强工具 _ 统信 _ 麒麟 _ 中科方德
  • (delphi11最新学习资料) Object Pascal 学习笔记---第14章泛型第3节(特定类约束)
  • 【postgresql初级使用】视图上的触发器instead of,替代计划的rewrite,实现不一样的审计日志
  • window.setInterval(func,interval)定时器
  • Einstein Summation 爱因斯坦求和 torch.einsum
  • TCP攻击是怎么实现的,如何防御?
  • Chrome DevTools开发者调试工具
  • 产品创新管理:从模仿到引领,中国企业的创新之路
  • Android 日志实时输出
  • JavaEE初阶---多线程编程(一.线程与进程)
  • react+vite创建
  • 软考 系统架构设计师系列知识点之杂项集萃(29)
  • [Qt开发]当我们在开发兼容高分辨率和高缩放比、高DPI屏幕的软件时,我们在谈论什么。
  • uniapp视频组件层级太高,解决方法使用subNvue原生子体窗口
  • java项目使用jsch下载ftp文件
  • 指针(初阶1)
  • MySQL实体类框架
  • 数据结构之初始泛型
  • 【网络编程开发】7.TCP可靠传输的原理
  • 视觉SLAM十四讲:从理论到实践(Chapter8:视觉里程计2)
  • C语言过度C++语法补充(面向对象之前语法)
  • 类和对象(二)(C++)
  • Chrome DevTools解密:成为前端调试大师的终极攻略
  • 【python】OpenCV—Cartoonify and Portray
  • 制作AI问答机器人:从0到1的完整指南
  • mysql 数据库datetime 类型,转换为DO里面的long类型后,只剩下年了,没有了月和日
  • 信息系统项目管理师0148:输出(9项目范围管理—9.3规划范围管理—9.3.3输出)
  • 解决 SQLyog 连接 MySQL 8 连不上和 SQLyog Trial 试用到期的问题