一、简介:freemarker是一个用java语言编写的模板引擎,它基于模板来生成文本输出。freemarker与web容器无关,即在web运行时,它并不知道servlet或http。它不仅可以用作表现层的实现技术,而且还用作生成HTML、jsp或java等。目前企业用的最多的是用其生成静态页面。
生成静态页面的使用,一般配合NGINX来使用。
二、简单使用:
前提:hudson.freemarker_ide_0.9.14插件加入到eclipse安装目录的dropins目录下,并重启eclipse。
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
@Test
public void test01() throws Exception{
//1、创建一个模板对象,名字随意,后缀随意
//2、创建一个Configuration对象
Configuration configuration = new Configuration(Configuration.getVersion());
//3、设置模板文件保存的目录
configuration.setDirectoryForTemplateLoading(new File("F:\\ECLIPSE\\data\\e3-parent\\e3-item-web\\src\\main\\webapp\\WEB-INF\\ftl"));
//4、模板文件的编码格式,一般是utf-8
configuration.setDefaultEncoding("utf-8");
//5、加载一个模板文件,创建一个模板对象
Template template = configuration.getTemplate("test.ftl");
//6、创建一个数据集,可以是pojo,也可以是Map,推荐使用map
Map map = new HashMap<>();
map.put("hello", "hello freemarker");
//7、创建一个writer对象,指定输出文件的路径及文件名,即本地磁盘路径
Writer fileWriter = new FileWriter(new File("C:\\Users\\ZH\\Desktop\\freemarker\\test.text"));
//8、生成静态页面
template.process(map, fileWriter);
//9、关闭流
fileWriter.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/475fbff838318b312004b23a22ad3506.png)