- 2022-08-08 发布 |
- 37.5 KB |
- 42页
文档介绍
[工学]freemarker
FreeMarker优秀的模板引擎\nFreeMarker特性使用纯Java编写能够生成各种文本:HTML、XML、RTF、Java源代码等可作为一个组件用于MVC模式的view层数据+模板=输出\nFreeMarker特性与容器无关,不需要Servlet环境可以应用于非Web应用程序环境可以在模板中使用JSP标记库强大的模板语言通用数据模型强大的XML处理能力FreeMarker是免费的\n应用实例Welcome${user}!
Ourlatestproduct: Ourlatestproduct:WelcomeBigJoe!
"}<#elseifmenu_has_next>${"|"}#if>#list>\nList指令实例<#assignx=3><#list1..xasi>${i}#list>输出:123\nInclude指令<#includefilename>or<#includefilenameoptions>说明:options包括parse和encodingparse:为true时,被包含的文件作用FTL模板处理,否则作为普通文本处理,默认为true。encoding\nInclude指令实例<#include"/common/navbar.html"parse=falseencoding="Shift_JIS"><#include“*/footer.ftl”>*代表本目录或任一上级目录。如有“/foo/bar/template.ftl”,则上例按如下顺序查找/foo/bar/footer.ftl、/foo/footer.ftl、/footer.ftl。如有<#include“*/commons/footer.ftl”>,会按/foo/bar/commons/footer.ftl、/foo/commons/footer.ftl、/commons/footer.ftl顺序查找。\nimport指令<#importpathashash>例:<#import"/libs/mylib.ftl"asmy><@my.copyrightdate="1999-2002"/>\nNoparse指令<#noparse>...#noparse>把其中的代码当普通字符串处理,如:<#noparse><#listanimalsasbeing>${being.name} ${being.price}Euros#list>#noparse>执行后是:<#listanimalsasbeing> ${being.name} ${being.price}Euros#list>\nAssign指令<#assignname=value>or<#assignname1=value1name2=value2...nameN=valueN>or<#assignsameasabove...innamespacehash>or<#assignname>capturethis#assign>or<#assignnameinnamespacehash>capturethis#assign>说明:定义或替换已存在的顶级变量。如:some_hash.subvar不行,而some_hash则可以。\nAssign指令实例<#assignseasons=["winter","spring","summer","autumn"]><#assigntest=test+1><#import"/mylib.ftl"asmy><#assignbgColor="red"inmy><#macromyMacro>foo#macro><#assignx><#list1..3asn>${n}<@myMacro/>#list>#assign>Numberofwords:${x?word_list?size}${x}。输出是:Numberofwords:61foo2foo3foo\nsetting指令<#settingname=value>用来设置数字日期格式等。如:${1.2}<#settinglocale="en_US">${1.2}输出:1,21.2\n用户定义指令<@macro_or_transfparam1=val1param2=val2...paramN=valN/>Or<@macro_or_transf...>...@macro_or_transf>说明:主要用来调用macro或transform\nmacro指令<#macronameparam1param2...paramN>...<#nestedloopvar1,loopvar2,...,loopvarN>...<#return>...#macro>说明:\nFunction指令<#functionnameparam1param2...paramN>...<#returnreturnValue>...#function>如:<#functionavgnums...><#localsum=0><#listnumsasnum><#localsum=sum+num>#list><#ifnums?size!=0><#returnsum/nums?size>#if>#function>${avg(10,20)}${avg(10,20,30,40)}${avg()!"N/A"}输出:1525N/A\nAttempt指令<#attempt>attemptblock<#recover>recoverblock#attempt>说明:若在遇到错误,则执行recoverblock\nAttempt指令实例Primarycontent<#attempt>Optionalcontent:${thisMayFails}<#recover>Ops!Theoptionalcontentisnotavailable.#attempt>Primarycontentcontinued\n数据类型stringnumberfordatebooleansequencehashnode(forXML)\nstringsubstringexp?substring(from,toExclusive)exp?substring(from)如:${‘abc’?substring(2)}结果是:bc;${'abc'?substring(1,2)}结果是:bcap_first:第一个单词的第一个字母大写。如:${“greenmouse”?cap_first}${“GreENmouse”?cap_first}${“-greenmouse”?cap_first}的结果是:GreenmouseGreENmouse-greenmouse\nstringdate,time,datetime:将字符串转换成日期时间。如:<#assigntest1="10/25/1995"?date("MM/dd/yyyy")><#assigntest2="15:05:30"?time("HH:mm:ss")><#assigntest3="1995-10-2503:05PM"?datetime("yyyy-MM-ddhh:mma")>${test1}${test2}${test3}输出结果是:Oct25,19953:05:30PMOct25,19953:05:00PM\nstringends_with:判断是否是以指定的字符串结束。如:redhead“?ends_with(”head“)将返回trueindex_of:返回指定子串出现的位置。如:“abcabc”?index_of(“bc”)返回1“abcabc”?index_of(“bc”,2)返回2last_index_of:返回指定串从后往前出现的位置。如:“abcabc“?last_index_of(”ab“)返回3"abcabc"?last_index_of("ab",2)返回0Length:字符数,即字符串的长度lower_case:转成小写left_pad:不满指定长度时,用指定字符填补(默认为空格)。如:[${“ab”?left_pad(5,“-”)}]输出:[---ab]\nstringright_pad:与前者相似contains:若包含指定子串,则返回true。如:<#if“piceous”?contains(“ice”)>Itcontains“ice”#if>输出:Itcontains"ice"replace。如:${“thisisacaracarus”?replace(“car”,“bulldozer”)}输出:thisisabulldozerabulldozerussplit。如:<#list“someMOOtestMOOtext”?split(“MOO”)asx>-${x}#list>输出:-some-test-text\nstringstarts_withtrim:去首尾空格upper_case:转大写\nnumberstring。如:<#assignanswer=42/>${answer}${answer?string}<#--thesameas${answer}-->${answer?string.number}${answer?string.currency}${answer?string.percent}输出:424242$42.004,200%\nnumber要指定数字格式,用如下方法:<#settingnumber_format="0.###E0"/>${1234}${12345?string("0.####E0")}输出:1.234E31.2345E4\ndatedatetimedatetime\nsequencesfirst:第一个元素last:最后一个元素seq_contains:判断是否包含指定字符串。如:<#assignx=["red",16,"blue","cyan"]>"blue":${x?seq_contains("blue")?string("yes","no")}"yellow":${x?seq_contains("yellow")?string("yes","no")}16:${x?seq_contains(16)?string("yes","no")}"16":${x?seq_contains("16")?string("yes","no")}输出:"blue":yes"yellow":no16:yes"16":no\nsequencesseq_index_of:返回指定值在sequences中首次出现的位置,若无,则返回-1。如:<#assigncolors=[“red”,“green”,“blue”]>${colors?seq_index_of(“blue”)}${colors?seq_index_of(“red”)}${colors?seq_index_of(“purple”)}输出:20-1也可用${names?seq_index_of(“Joe”,2)}这种形式,返回指定串第几次出现的位置。seq_last_index_of\nsequencesreversesizesort。如:<#assignls=["whale","Barbara","zeppelin","aardvark","beetroot"]?sort><#listlsasi>${i}#list>sort_by。如:<#assignls=[{“name”:“whale”,“weight”:2000},{“name”:“Barbara”,“weight”:53},{“name”:“zeppelin”,“weight”:-200},{“name”:“aardvark”,“weight”:30},{“name”:“beetroot”,“weight”:0.3}]>Orderbyname:<#listls?sort_by(“name”)asi>-${i.name}:${i.weight}#list>Orderbyweight:<#listls?sort_by(“weight”)asi>${i.name}:${i.weight}#list>\nsequences\n其它default:如果存在就用本生的值,不存在就用缺省值。如:<#assignseq=[‘a’,‘b’]>${seq[0]?default(‘-’)}${seq[1]?default(‘-’)}${seq[2]?default(‘-’)}${seq[3]?default(‘-’)}输出:ab--exists:变量存在返回true,则否返回falsehas_content:变量存在同时不为空时返回true,则否返回false
相关文章
您可能关注的文档