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的模板继承和列表处理能力,适用于构建复杂且可重用的页面结构。

相关推荐

  1. Freemarker

    2024-06-08 06:30:02       10 阅读
  2. SpringBoot整合FreeMarker

    2024-06-08 06:30:02       35 阅读
  3. SpringBoot整合FreeMarker模板引擎

    2024-06-08 06:30:02       35 阅读
  4. springboot_3.2_freemark_基础环境配置

    2024-06-08 06:30:02       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-08 06:30:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-08 06:30:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-08 06:30:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-08 06:30:02       20 阅读

热门阅读

  1. MySQL学习——获取数据库和表格的信息

    2024-06-08 06:30:02       10 阅读
  2. solidity的modifier修饰符

    2024-06-08 06:30:02       9 阅读
  3. 数据分析------统计学知识点(一)

    2024-06-08 06:30:02       10 阅读
  4. QT部署程序的三种方式

    2024-06-08 06:30:02       9 阅读
  5. hadoop命令大全

    2024-06-08 06:30:02       8 阅读
  6. 监控易监测对象及指标之:全面监控神通数据库

    2024-06-08 06:30:02       8 阅读
  7. Vue 数据更新了但页面没有更新

    2024-06-08 06:30:02       8 阅读
  8. 【二进制部署k8s-1.29.4】十、coredns的安装部署

    2024-06-08 06:30:02       9 阅读
  9. Linux-struct list_head的快速使用

    2024-06-08 06:30:02       8 阅读
  10. 调用plt函数报错not ‘KeyboardModifier’

    2024-06-08 06:30:02       11 阅读
  11. 理解和实现 LRU 缓存置换算法

    2024-06-08 06:30:02       8 阅读
  12. 【Numpy】04 深入理解NumPy的高级索引技术

    2024-06-08 06:30:02       7 阅读
  13. MYSQL内存占用查询语句

    2024-06-08 06:30:02       6 阅读