第六节 FreeMarker 使用模板生成静态页面

亮子 2023-09-14 01:58:53 17400 0 0 0

1、添加依赖

        <!-- freemarker 生成静态页 依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

2、配置文件

spring:
  # freemarker配置
  freemarker:
    cache: false #关闭模板缓存,方便测试
    settings:
      template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
    template-loader-path: classpath:/templates
    charset: UTF-8
    check-template-location: true
    suffix: .ftl
    content-type: text/html
    expose-request-attributes: true
    expose-session-attributes: true
    request-context-attribute: request

3、pom.xml文件

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.csv</include>
                    <include>**/*.vm</include>
                    <include>**/*.jpg</include>
                    <include>**/*.ftl</include>
                </includes>
            </resource>
        </resources>
    </build>

4、代码

package com.bw.pxx.user;

import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 军哥
 * @version 1.0
 * @description: TestFreeMarkerApp
 * @date 2023/9/14 9:19
 */

//@SpringBootTest
public class TestFreeMarkerApp {

    @Test
    public void makeHtml() {
        Configuration config = new Configuration(Configuration.VERSION_2_3_20);
        try {
            // 设置FreeMarker模板文件所在的目录
            // 方法1:使用绝对路径
//            config.setDirectoryForTemplateLoading(new File("D:\\code\\server-pxx-shop\\server-shop-user\\src\\main\\resources\\templates"));

            // 方法2:使用类路径
            config.setTemplateLoader(new ClassTemplateLoader(
                    this.getClass().getClassLoader(), "/templates/"
            ));

            // 获取模板
            Template template = config.getTemplate("index.ftl");

            // 设置模板参数
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("title", "名字");
            hashMap.put("content", "内容");

            // 渲染模板为静态文件内容
            String renderedContent = FreeMarkerTemplateUtils.processTemplateIntoString(template, hashMap);

            // 将渲染后的内容写入静态文件
            FileWriter writer = new FileWriter(new File("E:\\temp\\upload\\index.html"));
            writer.write(renderedContent);
            writer.close();

            return;
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
            return;
        }
    }

}