Freemarker The simplest example of a program

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.