Freemarker The simplest example of a program
Freemarker-2.3.18.tar.gz
Http://cdnetworks-kr-1.dl.sourceforge.net/project/freemarker/freemarker/2.3.18/freemarker-2.3.18.tar.gz
Freemarker-2.3.13.jar:
Link: http://pan.baidu.com/s/1eQVl9Zk Password: izs5
1. Create template objects by string, and perform interpolation processing
After execution, the console outputs the results:
Import freemarker.template.Template;
Import Java.io.OutputStreamWriter;
Import Java.io.StringReader;
Import Java.util.HashMap;
Import Java.util.Map;
/** * Freemarker Simplest example * * @author leizhimin 11-11-17 Morning 10:32 * * Public
class Test2 {public
static void main (Stri Ng[] args)
throws exception{ //Create a Template object
Template t = new Template (null, new StringReader ("Username: ${user}; URL: ${url}; Name: ${name} "), null);
Create the interpolated map map
= new HashMap ();
Map.put ("User", "Lavasoft");
Map.put ("url", "http://www.baidu.com/");
Map.put ("name", "Baidu");
Executes the interpolation and outputs to the specified output stream
t.process (map, new OutputStreamWriter (System.out)); }
User name: Lavasoft; url:http://www.baidu.com/;
Name: Baidu Process finished with exit code 0
2, through the file to create template objects, and perform interpolation operations
import freemarker.template.Configuration;
Import Freemarker.template.Template;
Import Java.io.File;
Import Java.io.OutputStreamWriter;
Import Java.util.HashMap;
Import Java.util.Map;
/** * Freemarker Simplest example * * @author leizhimin 11-11-14 pm 2:44 * * Public class Test {private Configuration cfg;
Template Configuration Object public void init () throws Exception {//Initialize Freemarker configuration//Create a Configuration instance cfg = new Configuration (); Sets the template folder location cfg.setdirectoryfortemplateloading (new File ("G:\\testprojects\\freemarkertest\\src") freemarker);
The public void process () throws Exception {//constructs the map map map of the populated data = new HashMap (); Map.put ("User", "Lavasoft");
Map.put ("url", "http://www.baidu.com/"); Map.put ("name", "Baidu");
Create a Template object Template t = cfg.gettemplate ("TEST.FTL"); Performs an interpolation operation on the stencil and outputs the t.process (map, new OutputStreamWriter (System.out)) to the established output stream;
public static void Main (string[] args) throws Exception {Test HF = new test (); Hf.init (); hf.process ();}}
Create a template file Test.ftl
After execution, the console outputs the following results: