Freemarker 最簡單的例子程序
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來創(chuàng)建模版對象,并執(zhí)行插值處理
執(zhí)行后,控制臺輸出結(jié)果:
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{ //創(chuàng)建一個(gè)模版對象 Template t = new Template(null, new StringReader("用戶名:${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)); } }
用戶名:lavasoft;URL: http://www.baidu.com/; 姓名: 百度 Process finished with exit code 0
2、通過文件來創(chuàng)建模版對象,并執(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最簡單的例子 * * @author leizhimin 11-11-14 下午2:44 */ public class Test { private Configuration cfg; //模版配置對象 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)建模版對象 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> 尊敬的用戶你好: 用戶名:${user}; URL: ${url}; 姓名: ${name}
執(zhí)行后,控制臺輸出結(jié)果如下:
<html> <head> <title>Welcome!</title> </head> <body> <h1>Welcome lavasoft!</h1> <p>Our latest product: <a >百度</a>! </body> </html> 尊敬的用戶你好: 用戶名:lavasoft; URL: http://www.baidu.com/; 姓名: 百度 Process finished with exit code 0
相關(guān)文章
SpringBoot四種讀取properties文件的方式(小結(jié))
這篇文章主要介紹了SpringBoot四種讀取properties文件的方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05SpringBoot JdbcTemplate批量操作的示例代碼
本篇文章主要介紹了SpringBoot JdbcTemplate批量操作的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04springboot實(shí)現(xiàn)異步調(diào)用@Async的示例
這篇文章主要介紹了springboot實(shí)現(xiàn)異步調(diào)用@Async的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12談?wù)凧ava中try-catch-finally中的return語句
我們知道return語句用在某一個(gè)方法中,一是用于返回函數(shù)的執(zhí)行結(jié)果,二是用于返回值為void類型的函數(shù)中,僅僅是一個(gè)return語句(return ;),此時(shí)用于結(jié)束方法的執(zhí)行,也即此return后的語句將不會(huì)被執(zhí)行,當(dāng)然,這種情況下return語句后不能再有其它的語句了2016-01-01