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.文字列を介してテンプレートオブジェクトを作成し、補間処理を実行します
実行後、コンソールは結果を出力します。
freemarker.template.templateをインポートします。 java.io.outputStreamWriterをインポートします。 java.io.stringreaderをインポートします。 java.util.hashmapをインポートします。 java.util.mapをインポートします。 /** * freemarkerの最も簡単な例 * * @author leizhimin 11-11-17 10:32 am */public class test2 {public static void main(string [] args)throws exception {//テンプレートオブジェクトテンプレートt = newテンプレート(null、new stringreader( "username:$ {user}; url:; url:; $ {name} ")、null); //補間マップマップマップ= new Hashmap()を作成します。 map.put( "user"、 "lavasoft"); map.put( "url"、 "http://www.baidu.com/"); map.put( "name"、 "baidu"); //指定された出力ストリームt.process(Map、new outputStreamWriter(System.out))に補間と出力を実行します); }}ユーザー名:lavasoft; url:http://www.baidu.com/;name:baidu processfinishedwithexitcode0
2。ファイルを介してテンプレートオブジェクトを作成し、補間操作を実行します
freemarker.template.configurationをインポートします。 freemarker.template.templateをインポートします。 java.io.fileをインポートします。 java.io.outputStreamWriterをインポートします。 java.util.hashmapをインポートします。 java.util.mapをインポートします。 / ** * freemarkerの最も簡単な例 * * @author leizhimin 11-11-14 2:44 pm */ public class test {private configuration cfg; //テンプレート構成オブジェクトpublic void init()スロー例外{// freemarker configurationを初期化//構成インスタンスCFG = new Configuration(); // freemarker cfg.setdirectoryfortemplateloading(new file( "// testprojects // freemarkertest // src")) } public void process()throws exception {//データマップに記入されたマップマップマップを作成します= new Hashmap(); map.put( "user"、 "lavasoft"); map.put( "url"、 "http://www.baidu.com/"); map.put( "name"、 "baidu"); //テンプレートオブジェクトテンプレートを作成しますt = cfg.getTemplate( "test.ftl"); //テンプレートで補間操作を実行し、処方出力ストリームt.process(Map、new outputStreamWriter(System.out))に出力を実行します。 } public static void main(string [] args)スロー例外{test hf = new test(); hf.init(); hf.process(); }}テンプレートファイルtest.ftlを作成します
<html> <head> <title>ようこそ! </body> </html> hello、dear user:username:$ {user}; url:$ {url};名前:$ {name}実行後、コンソール出力の結果は次のとおりです。
<html> <head> <title>ようこそ! </body> </html>こんにちは、親愛なるユーザー:ユーザー名:lavasoft; URL:http://www.baidu.com/;名前:baiduプロセスはexitコード0で終了しました