Package Com.sxt.test.freemarker;import java.io.file;import java.io.outputstreamwriter;import java.io.Writer; Import Java.util.arraylist;import java.util.date;import Java.util.hashmap;import Java.util.list;import Java.util.map;import Java.util.random;import Freemarker.template.configuration;import Freemarker.template.Template; Public classTest1 { Public Static voidMain (string[] args) throws Exception {//Creating a Freemarker configuration instanceConfiguration cfg =NewConfiguration (); Cfg.setdirectoryfortemplateloading (NewFile ("Templates")); //creating a data Modelmap<string, object> root =NewHashmap<string, object>(); Root.put ("User","Test User");//JavaBean can be converted automatically! Similar to the El expression we learned earlier! User U =NewUser (); U.setuname ("Little Red"); Root.put ("User1", u); User U2=NewUser ("Xiao Ming",NewAddress ("China","Beijing")); Root.put ("User2", U2);//map/list Containerlist<address> list =NewArraylist<address>(); List.add (NewAddress ("China","Beijing")); List.add (NewAddress ("China","Shanghai")); List.add (NewAddress ("United States","New York")); Root.put ("LST", list);//ordinary scalar??Root.put ("NUM0", -); Root.put ("B2",true); Root.put ("Date1",NewDate ()); Root.put ("Random",NewRandom (). Nextint ( -)); Root.put ("htm2","<b> Bold </b>");//Loading template filesTemplate T1 = cfg.gettemplate ("A.FTL");//displaying the generated dataWriter out=NewOutputStreamWriter (System. out); t1.process (Root, out); out. Flush ();}}
2./templates/a.ftl
General scalar testing:<#ifNum0==18>18 years old, grown up! </#if ><#--Digital--><#ifuser== "Test User" > Welcome you </#if ><#--string--><#ifB2>B2 is true!. </#if > <#--boolean value--${date1?string("YYYY-MM-DD HH:mm:ss")}<#--${date1.tostring ()}???? How to deal with the time?? ------------------------------------------------Common variables and JavaBean test: Hello, ${user},${user1.uname}, you have a good spirit today .!${user2.uname} from ${user2.addr.city}, where's the fun??-----------------------------------------------list data test: ${lst[0].city},${lst[1].city},${lst[2].city},------------------------------------------------built-in functions: ${HTM2?html}-------------------------------------------------To test the null value processing:<#--${sss} does not define this variable, it will report an exception! -${sss!} <#--does not define this variable, the default value is an empty string! -${sss!"ABC"} <#--does not define this variable, the default value is the string abc! -------------------------------------------------If statement test: ${user} Yes<#ifuser== "Teacher" > Our teacher </#if >------------------------------------------------if ElseStatement test:<#ifNUM0 GT 18> <#--is not used; Most of the time, Freemarker will interpret > as the end of the tag! -Pass! <#else>Not a lattice! </#if>---------------------------------------------------if Else ifElse Statement test:<#ifRandom GTE 90>Excellent! < #elseif random GTE the>Good! <#else>General! </#if>----------------------------------------------------To test the list directive:< #list LST asDizhi > <b>dizhi</b> <br/></#list >----------------------------------------------------To test the include directive:< #include"Included.txt"/>----------------------------------------------------< #macro m1> <#--definition Directives M1--<B>AAABBBCCC </b> <b>dddeeefff</b></#macro >< @m1/>< @m1/> <#--Call the above macro-------------- ------------------------------------------< #macro m2 a b c >${a}--${b}--${c}</#macro >< @m2 a="Test 1"b="Test 2"C="Test 3"/>----------------------------------------------------< #macro border> <table border=4cellspacing=0cellpadding=4><tr><td> < #nested > </td></tr></table> </#macro > < @border > in-table Capacity! </@border >-------------------------------------------------------To test the namespace:< #import"B.FTL" asBB/>< @bb. Copyright date="2010-2011"/>${bb.mail}< #assign mail="[email protected]"/>${mail}< #assign mail="[email protected]" inchbb/>${bb.mail}
Freemarker and Common Java