如今前后端分离,动静分离
使用freemarker实现动静分离,nginx处理静态资源文件,提高效率
加载jar包
1 <!-- freemarker -->
2 <dependency>
3 <groupId>org.freemarker</groupId>
4 <artifactId>freemarker</artifactId>
5 <version>${freemarker.version}</version>
6 </dependency>
@Test
public void testFreeMarker() throws Exception {
// 创建一个Configuration对象
Configuration configuration = new Configuration(Configuration.getVersion());
// config对象模板文件存放的路径
configuration.setDirectoryForTemplateLoading(new File("E:\\workspace_mars\\lee\\src\\main\\webapp\\WEB-INF\\ftl"));
// 设置config的默认字符集
configuration.setDefaultEncoding("utf-8");
// 获得模板对象, 指定模板文件名
Template template = configuration.getTemplate("hello.ftl");
// 创建模板数据集 可以是map对象或者pojo
Map root = new HashMap<>();
root.put("hello", "hello freemarker");
// 创建Writer对象 指定生成的文件保存的路径及文件名
Writer out = new FileWriter(new File("D:\\temp\\html\\hello.html"));
// 调用模板对象的process方法生成静态文件
template.process(root, out);
// 关闭writer
out.flush();
out.close();
}
成功后显示

这个比较简单
接下的文章会在项目中引用freemarker


Java技术交流群
网友评论