<!--freemarker template jar-related dependencies- <dependency> <groupid>org.freemarker</ groupid> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency>
PackageCom.jy.common.util;Importfreemarker.template.Configuration;Importfreemarker.template.Template;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.OutputStreamWriter;ImportJava.io.Writer;ImportJava.util.HashMap;ImportJava.util.Map;/*** Created by JY on 2018/3/21.*/ Public classFreemarkerutil { Public Static voidMain (string[] args) {createhtml (); } Public Static voidCreatehtml (String modelpath,string modelname,map<string,object>map) {Configuration cfg=NewConfiguration (); Try { //cfg.setdirectoryfortemplateloading (New File ("c:/my/aaa/bbb")); //set directory to read template filesCfg.setdirectoryfortemplateloading (NewFile (Modelpath));//set directory to read template files//Template t = cfg.gettemplate ("INDEX.FTL"); //read a template with the file name TEST.FTLTemplate t = cfg.gettemplate (modelname);//read a template with the file name INDEX.FTLMap Root=NewHashMap ();//Storing DataRoot.put ("id", 1); Root.put ("Name", "Zhangsan"); Writer out=NewOutputStreamWriter (NewFileOutputStream ("C:/my/aaa/bbb/a.html"), "UTF-8");//output Streamt.process (root, out);//dynamically load the data in root to test.html. The data is defined in the template. //t.process (map, out);//dynamically load the data in root to test.html. The data is defined in the template. }Catch(Exception e) {} System.out.println ("Create successfully!"); }}
Using Freemark to generate static HTML pages