Freemarker 最簡(jiǎn)單的例子程序?
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、通過(guò)String來(lái)創(chuàng)建模版對(duì)象,并執(zhí)行插值處理
執(zhí)行后,控制臺(tái)輸出結(jié)果:
import freemarker.template.Template; import java.io.OutputStreamWriter; import java.io.StringReader; import java.util.HashMap; import java.util.Map; /** * Freemarker最簡(jiǎn)單的例子 * * @author leizhimin 11-11-17 上午10:32 */ public class Test2 { public static void main(String[] args) throws Exception{ //創(chuàng)建一個(gè)模版對(duì)象 Template t = new Template(null, new StringReader("用戶(hù)名:${user};URL: ${url};姓名: ${name}"), null); //創(chuàng)建插值的Map Map map = new HashMap(); map.put("user", "lavasoft"); map.put("url", "http://www.baidu.com/"); map.put("name", "百度"); //執(zhí)行插值,并輸出到指定的輸出流中 t.process(map, new OutputStreamWriter(System.out)); } }
用戶(hù)名:lavasoft;URL: <a href="http://www.baidu.com/;">http://www.baidu.com/; </a>姓名: 百度 Process finished with exit code 0<br>
2、通過(guò)文件來(lái)創(chuàng)建模版對(duì)象,并執(zhí)行插值操作
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最簡(jiǎn)單的例子 * * @author leizhimin 11-11-14 下午2:44 */ public class Test { private Configuration cfg; //模版配置對(duì)象 public void init() throws Exception { //初始化FreeMarker配置 //創(chuàng)建一個(gè)Configuration實(shí)例 cfg = new Configuration(); //設(shè)置FreeMarker的模版文件夾位置 cfg.setDirectoryForTemplateLoading(new File("G:\\testprojects\\freemarkertest\\src")); } public void process() throws Exception { //構(gòu)造填充數(shù)據(jù)的Map Map map = new HashMap(); map.put("user", "lavasoft"); map.put("url", "http://www.baidu.com/"); map.put("name", "百度"); //創(chuàng)建模版對(duì)象 Template t = cfg.getTemplate("test.ftl"); //在模版上執(zhí)行插值操作,并輸出到制定的輸出流中 t.process(map, new OutputStreamWriter(System.out)); } public static void main(String[] args) throws Exception { Test hf = new Test(); hf.init(); hf.process(); } }
創(chuàng)建模版文件test.ftl
<html> <head> <title>Welcome!</title> </head> <body> <h1>Welcome ${user}!</h1> <p>Our latest product: <a href="${url}">${name}</a>! </body> </html> 尊敬的用戶(hù)你好: 用戶(hù)名:${user}; URL: ${url}; 姓名: ${name}
執(zhí)行后,控制臺(tái)輸出結(jié)果如下:
<html> <head> <title>Welcome!</title> </head> <body> <h1>Welcome lavasoft!</h1> <p>Our latest product: <a href="http://www.baidu.com/">百度</a>! </body> </html> 尊敬的用戶(hù)你好: 用戶(hù)名:lavasoft; URL: http://www.baidu.com/; 姓名: 百度 Process finished with exit code 0
? ?
更多Freemarker 最簡(jiǎn)單的例子程序相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

熱AI工具

Undress AI Tool
免費(fèi)脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線(xiàn)上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門(mén)文章

熱工具

記事本++7.3.1
好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6
視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版
神級(jí)程式碼編輯軟體(SublimeText3)
