The basic use of FreeMarker; the important thing is to use the three steps of using Freemarker!
The ftl file is a script! ! !
table of Contents
One: How to download and install FreeMarker:
Two: How to introduce freeMarker; FreeMarker Editor editor plug-in installation;
2. FreeMarker Editor editor plug-in installation
Three: FreeMarker actual coding example
One: How to download and install FreeMarker:
Visit: https://freemarker.apache.org/ ; FreeMarker is an open source project provided by Apache;
There are two ways to install FreeMarker: (1) the first way to manually download the jar package; (2) the other way through Maven;
Two: How to introduce freeMarker; FreeMarker Editor editor plug-in installation;
1. Introduce Freemarker:
2. FreeMarker Editor editor plug-in installation
Recommended installation: FreeMarker IDE from JBoss Tools 1.5
So far, everything is ready;
Three: FreeMarker actual coding example
FreeMarkerSample1 content: use FreeMarker, combined with templates for output;
(1) The operation of Freemarker is divided into three steps: loading template; creating data; generating output ;
package com.imooc.freemarker;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
public class FreeMarkerSample1 {
public static void main(String[] args) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
// TODO Auto-generated method stub
// 1.加载模板
// Configuration对象是FreeMarker的核心配置对象
// 1.1创建核心配置对象
Configuration config = new Configuration(Configuration.VERSION_2_3_31);// 因为我们下载的freemarker版本为2.3.31;所以这儿构造参数为2_3_31;
// 1.2设置加载的目录; 这儿的意思:在FreeMarkerSample1这个类所在包中去加载指定的ftl文件;第二个参数传递一个空字符串代表是当前包;
config.setClassForTemplateLoading(FreeMarkerSample1.class, "");
// 1.3得到模板对象;;;; 以为1.2中设置了,只去扫描FreeMarkerSample1这个类所在包中的ftl,所以1.3这一步getTemplate()参数只需要指定ftl文件名字就可以了;
Template t = config.getTemplate("sample1.ftl");
// 2.创建数据 ;;;;;对于Freemaker来说,其数据就是一个Map类型的对象;
// Map用来包含需要向模板中传入的数据
Map<String,Object> dataMap = new HashMap<String,Object>();
dataMap.put("site", "百度");
dataMap.put("url", "http://www.baidu.com/");
// 3.产生输出;;;这个输出,可以向文件输入,可以向控制台输出,可以向网络流中输出;向什么地方输出,取决于使用哪种类型的输出对象;
t.process(dataMap, new OutputStreamWriter(System.out));
// System.out:是向控制台输出的核心对象;System.out这个对象本质是一个PrintStream类型的对象,而process()方法的第二个参数需要是Writer类型的对象,所以使用new OutputStreamWriter(System.out),转换一下;
}
}
Then, when the following content is written in sample1.ftl:
${site}-${url}
At this time, the output of running FreeMarkerSample1 is:
百度-http://www.baidu.com/
……………………………………………………
Then, when the following content is written in sample1.ftl:
${site}~~~${url}
At this time, the output of running FreeMarkerSample1 is:
百度~~~http://www.baidu.com/
Through the above example, we can find one thing: the background part only writes the code and prepares the data, and does not care about how the data is displayed in the foreground;
The ftl file is responsible for how the data is displayed;
This seems to reflect the separation of data and display; separation of work, separation of "front and back" work, naturally the overall efficiency of the project can be improved! ! !
So, what??? to be added
Need other examples, continue to deepen~~~