基于模板生成html文件

基于模板生成html文件 

import com.lxw.pojo.Product;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

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


@SpringBootTest
@RunWith(SpringRunner.class)
public class TestFreemarker {

    //模型数据
    public Map getModelData() {
        HashMap map = new HashMap();
        map.put("user", "李白");
        Product product = new Product();
        product.setUrl("http://www.baidu.com");
        product.setName("百度");
        map.put("latestProduct", product);
        return map;
    }

    /**
     * 基于模板生成静态化html文件
     * @throws IOException
     * @throws TemplateException
     */
    @Test
    public void testGenerateHtml() throws IOException, TemplateException {
        //1 创建配置类
        Configuration configuration = new Configuration(Configuration.getVersion());
        //设置字符集
        configuration.setDefaultEncoding("utf-8");
        //2 设置模板路径
        String path = this.getClass().getResource("/templates/").getPath();
        configuration.setDirectoryForTemplateLoading(new File(path));
        //3 获取模板文件,该步骤必须在 2设置模板路径后才能找到
        Template template = configuration.getTemplate("user.html");

        //获取数据模型
        Map modelData = this.getModelData();
        //静态化, 将FreeMarker模板文件转换为字符串
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, modelData);
        //把字符串转换为输入流 流对象
        ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes("utf-8"));
        //模板输出地址
        FileOutputStream fileOutputStream = new FileOutputStream(new File
                ("D:\\yh\\zxjy\\code\\service\\freemarkertest\\src\\test\\java\\com\\lxw\\newUser.html"));

        //将一个输入流的内容复制到一个输出流中,无需手动编写读取和写入的代码
        IOUtils.copy(inputStream, fileOutputStream);
        inputStream.close(); //关闭流对象
        fileOutputStream.close();
    }


    /**
     * 基于模板字符串生成静态化文件
     * @throws IOException
     * @throws TemplateException
     */
    @Test
    public void testGenerateHtmlByString() throws IOException, TemplateException {
        //1 创建配置类,FreeMarker API 的主要入口点;封装了FreeMarker的配置设置
        Configuration configuration = new Configuration(Configuration.getVersion());
        //2 模板内容,这里测试时使用简单的字符串作为模板
        String templateString = "<html>\n" +
                "<head>\n" +
                "    <title>Welcome!</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<h1>Welcome ${user}!</h1>\n" +
                "</body>\n" +
                "</html>";
        //3 模板加载器
        StringTemplateLoader stringTemplateLoader = new StringTemplateLoader(); //创建加载器对象
        stringTemplateLoader.putTemplate("template", templateString);//将模板放入加载程序
        configuration.setTemplateLoader(stringTemplateLoader);//给配置设置加载器
        //得到模板
        Template template = configuration.getTemplate("template", "utf-8");

        //获取数据模型(自定义数据,一般为从数据库中查)
        HashMap modelData = new HashMap();
        modelData.put("user", "李白");
        //静态化内容,将FreeMarker模板文件转换为字符串
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, modelData);
        //把字符串转换为输入字节流
        ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes("utf-8"));
        //创建输出字节流,File指定输出路径,路径最后指定文件名称
        FileOutputStream fileOutputStream = new FileOutputStream(new File
                ("D:\\yh\\zxjy\\code\\service\\freemarkertest\\src\\test\\java\\com\\lxw\\newUser.html"));

        IOUtils.copy(inputStream, fileOutputStream);
        inputStream.close();
        fileOutputStream.close();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小天博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值