Home > php教程 > PHP开发 > Freemarker's simplest example program

Freemarker's simplest example program

高洛峰
Release: 2017-01-05 13:23:20
Original
1474 people have browsed it

Freemarker 最简单的例子程序 

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:

链接: http://pan.baidu.com/s/1eQVl9Zk 密码: izs5

1、通过String来创建模版对象,并执行插值处理

执行后,控制台输出结果:

import freemarker.template.Template; 
import java.io.OutputStreamWriter; 
import java.io.StringReader; 
import java.util.HashMap; 
import java.util.Map; 
/** * Freemarker最简单的例子 * * @author leizhimin 11-11-17 上午10:32 */
 public class Test2 {  
public static void main(String[] args) 
throws Exception{   //创建一个模版对象   
Template t = new Template(null, new StringReader("用户名:${user};URL: ${url};姓名:  ${name}"), null);  
 //创建插值的Map   
Map map = new HashMap();   
map.put("user", "lavasoft");   
map.put("url", "http://www.baidu.com/");  
 map.put("name", "百度");  
 //执行插值,并输出到指定的输出流中  
 t.process(map, new OutputStreamWriter(System.out));  } }
Copy after login
用户名:lavasoft;URL:    <a href="http://www.baidu.com/;">http://www.baidu.com/;
</a>姓名:  百度 Process finished with exit code 0<br>
Copy after login

2、通过文件来创建模版对象,并执行插值操作

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最简单的例子 * * @author leizhimin 11-11-14 下午2:44 */
public class Test { private Configuration cfg; 
//模版配置对象 public void init() throws Exception { 
//初始化FreeMarker配置 //创建一个Configuration实例 cfg = new Configuration();
 //设置FreeMarker的模版文件夹位置 
cfg.setDirectoryForTemplateLoading(new File("G:\\testprojects\\freemarkertest\\src")); } 
public void process() throws Exception { //构造填充数据的Map Map map = new HashMap(); 
 map.put("user", "lavasoft"); map.put("url", "http://www.baidu.com/"); 
 map.put("name", "百度"); //创建模版对象 Template t = cfg.getTemplate("test.ftl"); 
//在模版上执行插值操作,并输出到制定的输出流中 t.process(map, new OutputStreamWriter(System.out)); }
 public static void main(String[] args) 
throws Exception { Test hf = new Test(); hf.init(); hf.process(); } }
Copy after login

创建模版文件test.ftl

<html> <head> <title>Welcome!</title> </head>
<body> <h1>Welcome ${user}!</h1> <p>Our latest product: <a href="${url}">${name}</a>! </body>
 </html> 尊敬的用户你好: 用户名:${user}; URL: ${url}; 姓名:  ${name}
Copy after login

执行后,控制台输出结果如下:

<html> <head> <title>Welcome!</title> </head>
 <body> <h1>Welcome lavasoft!</h1> <p>Our latest product: <a href="http://www.baidu.com/">百度</a>! </body> </html>
尊敬的用户你好: 用户名:lavasoft; URL: http://www.baidu.com/; 姓名:  百度 Process finished with exit code 0
Copy after login

   

更多Freemarker 最简单的例子程序相关文章请关注PHP中文网!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template