1. template file static.html
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
Description: ${description}
<br/>
Collection size: ${namelist?size}
<br/>
Iterate the list collection:
<br/>
< #list NameList as names>
This is the first ${names_index+1} person, called: <label style= "color:red" >${names}</label>
If judgment:
<br/>
< #if (names== "Chen Revenge") >
His weapon is: 15 ~ ~
< #elseif (names== "Yu text") > <#--Notice there is no return but at the end--
His weapon is: the Shaft sword ~
< #else >
Her trick is: Poison ~ ~
</#if >
<br/>
</#list >
Iteration Map Collection:
<br/>
< #list Weaponmap?keys as key>
Key--->${key}<br/>
Value----->${weaponmap[key]! (" Null ")}
<#--
Fremarker does not support NULL, can be used! Instead of an empty value.
In fact, you can also give a default value
Value-----${weaponmap[key]?default ("null")}
You can also decide whether to null before the output
< #if Weaponmap[key]?? ></#if > can
-
<br/>
</#list >
</body>
2. Generate the Code:
Package com.xijie.test;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;
Import Java.io.Writer;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import freemarker.template.Configuration;
Import Freemarker.template.DefaultObjectWrapper;
Import Freemarker.template.Template;
Import freemarker.template.TemplateException;
/**
* @description
* @author Changgui Pan
* @date July 19, 2018 11:38:53
*/
public class Freemarkertest {
/**
* @description
* @author Changgui Pan
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
try {
Create an appropriate Configration object
Configuration configuration = new configuration ();
Configuration.setdirectoryfortemplateloading (New File ("c:\\mywork\\workspace\\xijie\\webroot\\html"));
Configuration.setdirectoryfortemplateloading (New File ("./webroot/html"));
Configuration.setobjectwrapper (New Defaultobjectwrapper ());
Configuration.setdefaultencoding ("UTF-8"); This must be set, or it will be garbled in the generated page
Gets or creates a template.
Template template = Configuration.gettemplate ("static.html");
map<string, object> parammap = new hashmap<string, object> ();
Parammap.put ("description", "I'm learning to use Freemarker to generate static files!") ");
list<string> namelist = new arraylist<string> ();
Namelist.add ("Chen Revenge");
Namelist.add ("Jade Son");
Namelist.add ("Yu text extension");
Parammap.put ("NameList", namelist);
map<string, object> weaponmap = new hashmap<string, object> ();
Weaponmap.put ("First", "Xuan Jian");
Weaponmap.put ("Second", "Kong Tong Printing");
Weaponmap.put ("Third", "Nu wa Stone");
Weaponmap.put ("Fourth", "Shen Nong Ding");
Weaponmap.put ("Fifth", "Fuxi Qin");
Weaponmap.put ("Sixth", "Kunlun Mirror");
Weaponmap.put ("seventh", null);
Parammap.put ("Weaponmap", Weaponmap);
Writer writer = new OutputStreamWriter (New FileOutputStream ("success.html"), "UTF-8");
Template.process (Parammap, writer);
System.out.println ("Congratulations, build Success ~ ~");
} catch (IOException e) {
E.printstacktrace ();
} catch (Templateexception e) {
E.printstacktrace ();
}
}
}
Java uses Freemarker to generate static HTML pages