基于模板生成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();
}
}