First import the package, freemarker.jar:freemarker-2.3.18.tar.gzhttp://cdnetworks-kr-1.dl.sourceforge.net/project/freemarker/ Freemarker/2.3.18/freemarker-2.3.18.tar.gz of course, if it is Maven project, can be downloaded from the warehouse, here do not repeat; 1. Create a Template object by string and perform interpolation processingImportFreemarker.template.Template;
ImportJava.io.OutputStreamWriter;
ImportJava.io.StringReader;
ImportJava.util.HashMap;
ImportJava.util.Map;
/**
* The simplest example of freemarker
*
* @author leizhimin 11-11-17 10:32
*/
PublicclassTest2 {
PublicStaticvoidMain (string[] args)throwsexception{
//Create a Template object
Template T =NewTemplate (NULL,NewStringReader ("User name: ${user}; URL: ${url}; Name: ${name} "),NULL);
//Create an interpolated map
Map map =NewHashMap ();
Map.put ("User","Lavasoft");
Map.put ("url","http://www.baidu.com/");
Map.put ("name", "Baidu");
//performs interpolation and outputs to the specified output stream
T.process (map, new OutputStreamWriter (System.out));
}
}after execution, the console outputs the result:User name: Lavasoft; url:http://www.baidu.com/; Name: Baidu
Process finished with exit code 0 2, creating a template object from a file and performing an interpolation operationImportFreemarker.template.Configuration;
ImportFreemarker.template.Template;
ImportJava.io.File;
ImportJava.io.OutputStreamWriter;
ImportJava.util.HashMap;
ImportJava.util.Map;
/**
* The simplest example of freemarker
*
* @author leizhimin 11-11-14 2:44
*/
PublicclassTest {
PrivateConfiguration CFG;//Template Configuration Object
PublicvoidInit ()throwsException {
//Initialize Freemarker configuration
//Create a configuration instance
CFG =NewConfiguration ();
//Set Freemarker Template folder location
Cfg.setdirectoryfortemplateloading (NewFile ("G:\\TESTPROJECTS\\FREEMARKERTEST\\SRC"));
}
PublicvoidProcess ()throwsException {
//construct map to populate data
Map map =NewHashMap ();
Map.put ("User","Lavasoft");
Map.put ("url","http://www.baidu.com/");
Map.put ("Name","Baidu");
//Create template Object
Template t = cfg.gettemplate ("TEST.FTL");
//Perform interpolation operations on the template and output to the output stream as set
T.process (Map,NewOutputStreamWriter (System.out));
}
PublicStaticvoidMain (string[] args)throwsException {
Test HF =NewTest ();
Hf.init ();
Hf.process ();
}
}Creating a template file Test.ftl<HTML>
<Head>
<title>welcome! </title>
</head>
<Body>
<H1>Welcome ${user}! </H1>
<P>Our latest product:
<ahref= "${url}">${name} </a> !
</body>
</html>
Dear User Hello:
User name: ${user};
URL: ${url};
Name: ${name} After execution, the console output results are as follows:<title>Welcome!</title>
<body>
<p>our Latest Product:
<a href= "http://www.baidu.com/" > Baidu </a>!
</body>
Dear User Hello:
User name: Lavasoft;
url:http://www.baidu.com/;
Name: Baidu
Process finished with exit code 0
Transferred from: http://lavasoft.blog.51cto.com/62575/716825/
Freemarker The simplest example of a program