Freemarker is the simplest example program

  • 2020-05-10 18:05:04
  • OfStack

The simplest example of an Freemarker 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 / 1 eQVl9Zk password: izs5

1. Create template objects through String and perform interpolation

After execution, the console outputs the result:


import freemarker.template.Template; 
import java.io.OutputStreamWriter; 
import java.io.StringReader; 
import java.util.HashMap; 
import java.util.Map; 
/** * Freemarker The simplest example  * * @author leizhimin 11-11-17  In the morning 10:32 */
 public class Test2 {  
public static void main(String[] args) 
throws Exception{   // create 1 Template object    
Template t = new Template(null, new StringReader(" User name: ${user};URL :  ${url}; Name:   ${name}"), null);  
 // Create interpolated Map   
Map map = new HashMap();   
map.put("user", "lavasoft");   
map.put("url", "http://www.baidu.com/");  
 map.put("name", " baidu ");  
 // Performs 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 The simplest example  * * @author leizhimin 11-11-14  In the afternoon 2:44 */ 
public class Test { private Configuration cfg; 
// Template configuration object  public void init() throws Exception { 
// Initialize the FreeMarker configuration  // create 1 a Configuration The instance  cfg = new Configuration();
 // Set up the FreeMarker Template folder location  
cfg.setDirectoryForTemplateLoading(new File("G:\\testprojects\\freemarkertest\\src")); } 
public void process() throws Exception { // Construct the one that fills the data Map Map map = 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"); 
// Perform interpolation on the template and output to the specified output stream  t.process(map, new OutputStreamWriter(System.out)); }
 public static void main(String[] args) 
throws Exception { Test hf = new Test(); hf.init(); hf.process(); } }

Create the template file test.ftl


<html> <head> <title>Welcome!</title> </head> 
<body> <h1>Welcome ${user}!</h1> <p>Our latest product: <a href="${url}">${name}</a>! </body>
 </html>  Dear users,   User name: ${user}; URL :  ${url};  Name:   ${name}

After execution, the console output results are as follows:


<html> <head> <title>Welcome!</title> </head>
 <body> <h1>Welcome lavasoft!</h1> <p>Our latest product: <a href="http://www.baidu.com/"> baidu </a>! </body> </html> 
 Dear users,   User name: lavasoft; URL :  http://www.baidu.com/;  Name:   baidu  Process finished with exit code 0



Related articles: