freemarker标签使用

论坛 期权论坛 脚本     
匿名网站用户   2020-12-19 18:41   33   0
FreeMarker是一个用Java编写的模板引擎,主要用来生成HTML Web页面,特别是基于MVC模式的应用程序。虽然FreeMarker具有一些编程的能力,但不像PHP,通常由Java程序准备要显示的数据,由FreeMarker模板生成页面。 FreeMarker可以作为Web应用框架一个组件,但它与容器无关,在非Web应用程序环境也能工作的很好。 FreeMarker适合作为MVC的视图组件,还能在模板中使用JSP标记库。
Java代码 复制代码
  1. <html>
  2. <head>
  3. <title>Welcome!</title>
  4. </head>
  5. <body>
  6. <h1>Welcome${user}!</h1>
  7. <p>Ourlatestproduct:
  8. <ahref="${latestProduct.url}">${latestProduct.name}</a>!
  9. </body>
  10. </html>
  1. <html>
  2. <head>
  3. <title>Welcome!</title>
  4. </head>
  5. <body>
  6. <h1>Welcome${user}!</h1>
  7. <p>Ourlatestproduct:
  8. <ahref="${latestProduct.url}">${latestProduct.name}</a>!
  9. </body>
  10. </html>

上面的例子中,在简单的HTML中加入了一些由${…}包围的特定FreeMarker的指令,这个文件就称为模板了。而user、latestProduct.url和latestProduct.name来自于数据模型,由Java程序提供,模板设计者就不用关心数据从哪来的。
FreeMarker模板中可以包括下面四种特定部分:
一)文本:直接输出

二)FTL标记(FreeMarker模板语言标记):类似于HTML标记,名字前加#(有些以@开始,用户自定义标记)予以区分,不会输出。
1.字符串:使用单引号或双引号限定;如果包含特殊字符需要转义符:${"It's /"quoted/" andthis is a backslash: //"}
有一类特殊的字符串:${r"C:/foo/bar"},输出结构为:C:/foo/bar,在引号前面加r被认为是纯文本。

2.数字:直接输入,不需要引号。${08}, ${+8}, ${8.00} and ${8} 都是相同的

3.布尔值:true和false,不使用引号

4.Sequences(序列)
a.由逗号分隔的变量列表,由方括号限定,类似java中的一维数组:
例一:["winter", "spring", "summer", "autumn"]
例二:[2 + 2, [1, 2, 3, 4], "whatnot"] (可以嵌套)
例三:2..5,等同于[2, 3, 4, 5];5..2,等同于[5,4,3,2]。
注意方括号是不需要的。 (另外写法)
b.获取Sequence(序列)中元素-使用[startindex..endindex]
例如:seq中存储了"a", "b", "c", "d","e",
那么seq[1..2]包含了b和c两个值。
c.Sequences(序列)元素的遍历
<#list ["Joe", "Fred"] + ["Julia", "Kate"] as user>
${user}
</#list>
5.Hashes(散列)
由逗号分隔的键-值列表,由大括号限定,键和值之间用冒号分隔:{"name":"green mouse", "price":150},键和值都是表达式,但是键必须是字符串。
获取变量-${variable},变量名只能是字母、数字、下划线、$、@和#的组合,且不能以数字开头。可以使用.variablename语法访问FreeMarker内置变量。 下列表达式是等价的:
book.author.name
book["author"].name
book.author.["name"]
book["author"]["name"]

6.字符串操作
{"Hello ${user}!"} <==> ${"Hello " + user + "!"}
${"${user}${user}${user}${user}"} <==> ${user + user + user + user}
${…}只能在文本中使用,下面是错误的代码:
<#if ${isBig}>Wow!</#if>
<#if "${isBig}">Wow!</#if> //此处的代码也是错误的,因为if指令需要的是boolean,实际的却是个字符串
子字符串的操作,
<#assign user="Big Joe">
${user[0]}${user[4]} <==> BJ
${user[1..4]} <==> ig J
注意: 操作符两边必须是数字;使用"+"时,如果一边是数字,一边是字符串,就会自动将数字转换为字符串。

7.使用内建的指令int获得整数部分:
${(x/2)?int}
${1.1?int}
${1.999?int}
${-1.1?int}
${-1.999?int}

8.比较操作符-<#if expression>...</#if>
1.)使用=(或==,完全相等)测试两个值是否相等,使用!= 测试两个值是否不相等
2.)=和!=两边必须是相同类型的值,否则会产生错误,例如<#if 1 = "1">会引起错误
3.)Freemarker是精确比较,所以"x"、"x "和"X"是不相等的
4.)对数字和日期可以使用<、<=、>和>=,但不能用于字符串
5.)由于Freemarker会将>解释成FTL标记的结束字符,所以对于>和>=可以使用括号来避免这种情况,例如<#if (x > y)>,另一种替代的方法是,使用lt、lte、gt和gte来替代<、<=、>和>=
6.)逻辑操作符-&&(and)、||(or)、!(not),只能用于布尔值,否则会产生错误
<#if x < 12 && color = "green">
We have less than 12 things, and they are green.
</#if>
<#if !hot> <#-- here hot must be a boolean -->
It's not hot.
</#if>

9.内置函数-用法类似访问hash(散列)的子变量,只是使用"?"替代".",
例如:user?upper_case
下面列出常用的一些函数:
对于字符串
html-对字符串进行HTML编码
cap_first-使字符串第一个字母大写
lower_case-将字符串转换成小写
trim-去掉字符串前后的空白字符
对于Sequences(序列)
size-获得序列中元素的数目
对于数字
int-取得数字的整数部分(如-1.9?int的结果是-1)

10.方法的调用
${repeat("What", 3)}
${repeat(repeat("x", 2), 3) + repeat("What", 4)?upper_case}
结果:
WhatWhatWhat
xxxxxxWHATWHATWHATWHAT

11.操作符优先顺序
后缀 [subvarName] [subStringRange] . (methodParams)
一元 +expr、-expr、!
内建 ?
乘法 *、 / 、%
加法 +、-
关系 <、>、<=、>=(lt、lte、gt、gte)
相等 =、!=
逻辑 &&
逻辑 ||
数字范围 ..

12.一些常用控制语句
1)if, else, elseif
Java代码 复制代码
  1. <#ifx==1>
  2. xis1
  3. <#ify==1>
  4. andyis1too
  5. <#else>
  6. butyisnot
  7. </#if>
  8. <#else>
  9. xisnot1
  10. <#ify<0>
  11. andyislessthan0
  12. </#if>
  13. </#if>
  1. <#ifx==1>
  2. xis1
  3. <#ify==1>
  4. andyis1too
  5. <#else>
  6. butyisnot
  7. </#if>
  8. <#else>
  9. xisnot1
  10. <#ify<0>
  11. andyislessthan0
  12. </#if>
  13. </#if>

2)switch, case, default, break
<#switch value>
Java代码 复制代码
  1. <#switchbeing.size>
  2. <#case"small">
  3. Thiswillbeprocessedifitissmall
  4. <#break>
  5. <#case"medium">
  6. Thiswillbeprocessedifitismedium
  7. <#break>
  8. <#case"large">
  9. Thiswillbeprocessedifitislarge
  10. <#break>
  11. <#default>
  12. Thiswillbeprocessedifitisneither
  13. </#switch>
  1. <#switchbeing.size>
  2. <#case"small">
  3. Thiswillbeprocessedifitissmall
  4. <#break>
  5. <#case"medium">
  6. Thiswillbeprocessedifitismedium
  7. <#break>
  8. <#case"large">
  9. Thiswillbeprocessedifitislarge
  10. <#break>
  11. <#default>
  12. Thiswillbeprocessedifitisneither
  13. </#switch>

3) list, break
Java代码 复制代码
  1. <#listsequenceasitem>
  2. ${item}
  3. <#ifitem="xx"><#break></#if>
  4. </#list>
  1. <#listsequenceasitem>
  2. ${item}
  3. <#ifitem="xx"><#break></#if>
  4. </#list>

4)include
Java代码 复制代码
  1. <#include"/common/navbar.html"parse=falseencoding="Shift_JIS">
  1. <#include"/common/navbar.html"parse=falseencoding="Shift_JIS">

5)import
Java代码 复制代码
  1. <#import"/libs/mylib.ftl"asmy>
  2. <@my.copyrightdate="1999-2002"/>
  1. <#import"/libs/mylib.ftl"asmy>
  2. <@my.copyrightdate="1999-2002"/>

Note: that it is possible to automatically do the commonly used imports for all templates, with the "auto imports" setting of Configuration.
6)noparse
Java代码 复制代码
  1. <#noparse>
  2. <#listanimalsasbeing>
  3. <tr><td>${being.name}<td>${being.price}Euros
  4. </#list>
  5. </#noparse>
  1. <#noparse>
  2. <#listanimalsasbeing>
  3. <tr><td>${being.name}<td>${being.price}Euros
  4. </#list>
  5. </#noparse>

7)compress
Java代码 复制代码
  1. <#assignx="moo/n/n">
  2. (<#compress>
  3. 12345
  4. ${moo}
  5. testonly
  6. Isaid,testonly
  7. </#compress>)
  1. <#assignx="moo/n/n">
  2. (<#compress>
  3. 12345
  4. ${moo}
  5. testonly
  6. Isaid,testonly
  7. </#compress>)

8)escape, noescape
Java代码 复制代码
  1. <#assignx="<test>">
  2. <#macrom1>
  3. m1:${x}
  4. </#macro>
  5. <#escapexasx?html>
  6. <#macrom2>m2:${x}</#macro>
  7. ${x}
  8. <@m1/>
  9. </#escape>
  10. ${x}
  11. <@m2/>
  12. <#escapexasx?html>
  13. From:${mailMessage.From}
  14. Subject:${mailMessage.Subject}
  15. <#noescape>Message:${mailMessage.htmlFormattedBody}</#noescape>
  16. ...
  17. </#escape>
  18. <#escapexasx?html>
  19. CustomerName:${customerName}
  20. Itemstoship:
  21. <#escapexasitemCodeToNameMap[x]>
  22. ${itemCode1}
  23. ${itemCode2}
  24. ${itemCode3}
  25. ${itemCode4}
  26. </#escape>
  27. </#escape>
  1. <#assignx="<test>">
  2. <#macrom1>
  3. m1:${x}
  4. </#macro>
  5. <#escapexasx?html>
  6. <#macrom2>m2:${x}</#macro>
  7. ${x}
  8. <@m1/>
  9. </#escape>
  10. ${x}
  11. <@m2/>
  12. <#escapexasx?html>
  13. From:${mailMessage.From}
  14. Subject:${mailMessage.Subject}
  15. <#noescape>Message:${mailMessage.htmlFormattedBody}</#noescape>
  16. ...
  17. </#escape>
  18. <#escapexasx?html>
  19. CustomerName:${customerName}
  20. Itemstoship:
  21. <#escapexasitemCodeToNameMap[x]>
  22. ${itemCode1}
  23. ${itemCode2}
  24. ${itemCode3}
  25. ${itemCode4}
  26. </#escape>
  27. </#escape>

9)assign
Java代码 复制代码
  1. <#import"/mylib.ftl"asmy>
  2. <#assign
  3. seasons=["winter","spring","summer","autumn"]
  4. test=test+1
  5. bgColor="red"inmy
  6. >
  7. <#macromyMacro>foo</#macro>
  8. <#assignx>
  9. <#list1..3asn>
  10. ${n}<@myMacro/>
  11. </#list>
  12. </#assign>
  13. Numberofwords:${x?word_list?size}
  14. ${x}
  15. <#assignx="Hello${user}!">
  1. <#import"/mylib.ftl"asmy>
  2. <#assign
  3. seasons=["winter","spring","summer","autumn"]
  4. test=test+1
  5. bgColor="red"inmy
  6. >
  7. <#macromyMacro>foo</#macro>
  8. <#assignx>
  9. <#list1..3asn>
  10. ${n}<@myMacro/>
  11. </#list>
  12. </#assign>
  13. Numberofwords:${x?word_list?size}
  14. ${x}
  15. <#assignx="Hello${user}!">

10)global
Java代码 复制代码
  1. <#globalname=value>
  2. or
  3. <#globalname1=value1name2=value2...nameN=valueN>
  4. or
  5. <#globalname>
  6. capturethis
  7. </#global>
  1. <#globalname=value>
  2. or
  3. <#globalname1=value1name2=value2...nameN=valueN>
  4. or
  5. <#globalname>
  6. capturethis
  7. </#global>

11)local
note:it is similar to assign directive, but it creates or replaces local variables. This works in macro definition bodies only.
Java代码 复制代码
  1. <#localname=value>
  2. or
  3. <#localname1=value1name2=value2...nameN=valueN>
  4. or
  5. <#localname>
  6. capturethis
  7. </#local>
  1. <#localname=value>
  2. or
  3. <#localname1=value1name2=value2...nameN=valueN>
  4. or
  5. <#localname>
  6. capturethis
  7. </#local>

12)setting
note:The supported settings are:locale,number_format,boolean_format,
date_format, time_format, datetime_format,time_zone,url_escaping_charset,classic_compatible
Java代码 复制代码
  1. ${1.2}
  2. <#settinglocale="en_US">
  3. ${1.2}
  1. ${1.2}
  2. <#settinglocale="en_US">
  3. ${1.2}

13) (<@...>)
Java代码 复制代码
  1. <@myRepeatMacrocount=4;x,last>
  2. ${x}.Something...<#iflast>Thiswasthelast!</#if>
  3. </@myRepeatMacro>
  1. <@myRepeatMacrocount=4;x,last>
  2. ${x}.Something...<#iflast>Thiswasthelast!</#if>
  3. </@myRepeatMacro>

14)macro, nested, return
Java代码 复制代码
  1. <#macroimgsrcextra...>
  2. <imgsrc="/context${src?html}"
  3. <#listextra?keysasattr>
  4. ${attr}="${extra[attr]?html}"
  5. </#list>
  6. >
  7. </#macro>
  8. <@imgsrc="/images/test.png"width=100height=50alt="Test"/>
  9. <#macrorepeatcount>
  10. <#list1..countasx>
  11. <#nestedx,x/2,x==count>
  12. </#list>
  13. </#macro>
  14. <@repeatcount=4;c,halfc,last>
  15. ${c}.${halfc}<#iflast>Last!</#if>
  16. </@repeat>
  17. <#macrotest>
  18. Testtext
  19. <#return>
  20. Willnotbeprinted.
  21. </#macro>
  22. <@test/>
  1. <#macroimgsrcextra...>
  2. <imgsrc="/context${src?html}"
  3. <#listextra?keysasattr>
  4. ${attr}="${extra[attr]?html}"
  5. </#list>
  6. >
  7. </#macro>
  8. <@imgsrc="/images/test.png"width=100height=50alt="Test"/>
  9. <#macrorepeatcount>
  10. <#list1..countasx>
  11. <#nestedx,x/2,x==count>
  12. </#list>
  13. </#macro>
  14. <@repeatcount=4;c,halfc,last>
  15. ${c}.${halfc}<#iflast>Last!</#if>
  16. </@repeat>
  17. <#macrotest>
  18. Testtext
  19. <#return>
  20. Willnotbeprinted.
  21. </#macro>
  22. <@test/>

15)function, return

Java代码 复制代码
  1. <#functionavgnums...>
  2. <#localsum=0>
  3. <#listnumsasnum>
  4. <#localsum=sum+num>
  5. </#list>
  6. <#ifnums?size!=0>
  7. <#returnsum/nums?size>
  8. </#if>
  9. </#function>
  10. ${avg(10,20)}
  11. ${avg(10,20,30,40)}
  12. ${avg()!"N/A"}
  1. <#functionavgnums...>
  2. <#localsum=0>
  3. <#listnumsasnum>
  4. <#localsum=sum+num>
  5. </#list>
  6. <#ifnums?size!=0>
  7. <#returnsum/nums?size>
  8. </#if>
  9. </#function>
  10. ${avg(10,20)}
  11. ${avg(10,20,30,40)}
  12. ${avg()!"N/A"}

16).flush
17)stop
18)ftl
19)t, lt, rt
20)nt
21)attempt, recover

Java代码 复制代码
  1. Primarycontent
  2. <#attempt>
  3. Optionalcontent:${thisMayFails}
  4. <#recover>
  5. Ops!Theoptionalcontentisnotavailable.
  6. </#attempt>
  7. Primarycontentcontinued
  1. Primarycontent
  2. <#attempt>
  3. Optionalcontent:${thisMayFails}
  4. <#recover>
  5. Ops!Theoptionalcontentisnotavailable.
  6. </#attempt>
  7. Primarycontentcontinued

22)visit, recurse, fallback


三. Interpolation:由${...}或#{...}两种类型,输出计算值,可以自定义输出的格式

1)string的操作:
substring
cap_first
uncap_first
capitalize
chop_linebreak
date, time, datetime
ends_with
html
groups
index_of
j_string
js_string
last_index_of
length
lower_case
left_pad
right_pad
contains
matches
number
replace
rtf
url
split
starts_with
string (when used with a string value)
trim
upper_case
word_list
xml

2)numbers的操作:
c
string (when used with a numerical value)
round, floor, ceiling


3)dates的操作:
string (when used with a date value)
date, time, datetime

4)booleans的操作:
string (when used with a boolean value)
5)sequences的操作:
first
last
seq_contains
seq_index_of
seq_last_index_of
reverse
size
sort
sort_by
chunk

6)hashes的操作:
keys
values

7)nodes 的操作:
children
parent
root
ancestors
node_name
node_type
node_namespace

四.) 注释:<#--和-->





下面是一个常用的模板例子:
例一:
Java代码 复制代码
  1. <p>Wehavetheseanimals:
  2. <tableborder=1>
  3. <tr><th>Name<th>Price
  4. <#listanimalsasbeing>
  5. <tr>
  6. <td>
  7. <#ifbeing.size="large"><b></#if>
  8. ${being.name}
  9. <#ifbeing.size="large"></b></#if>
  10. <td>${being.price}Euros
  11. </#list>
  12. </table>
  13. <#include"/copyright_footer.html">
  1. <p>Wehavetheseanimals:
  2. <tableborder=1>
  3. <tr><th>Name<th>Price
  4. <#listanimalsasbeing>
  5. <tr>
  6. <td>
  7. <#ifbeing.size="large"><b></#if>
  8. ${being.name}
  9. <#ifbeing.size="large"></b></#if>
  10. <td>${being.price}Euros
  11. </#list>
  12. </table>
  13. <#include"/copyright_footer.html">

注意点:
1.) FreeMarker是区分大小写的;
2.) FTL标记不能位于另一个FTL标记内部,例如:<#if <#include 'foo'>='bar'>...</if>;
3.) ${…}只能在文本中使用;
4.) 多余的空白字符会在模板输出时去除;
5.) 如果使用的指令不存在,会产生一个错误消息。

例子2。
Java代码 复制代码
  1. <%@tagliburi="/WEB-INF/fmtag.tld"prefix="fm"%>
  2. <jsp:useBeanid="mybean"class="<SPANclass=hilite1>freemarker</SPAN>.examples.jsp.SimpleBean"/>
  3. <jsp:useBeanid="mybeanreq"class="<SPANclass=hilite1>freemarker</SPAN>.examples.jsp.SimpleBean"scope="request"/>
  4. <fm:template>
  5. <html>
  6. <head>
  7. <title><SPANclass=hilite1>FreeMarker</SPAN>JSPExample</title>
  8. </head>
  9. <body>
  10. <h1><SPANclass=hilite1>FreeMarker</SPAN>JSPexample</h1>
  11. <hr>
  12. <p>
  13. ThispageisaJSPpage,yetmostofitscontentsisgeneratedusing
  14. a<SPANclass=hilite1>FreeMarker</SPAN>template.Thebelowlinesaretheoutputofcalling
  15. propertiesonaJSP-declaredbeanfromthe<SPANclass=hilite1>FreeMarker</SPAN>template:
  16. </p>
  17. <#assignmybean=page.mybean>
  18. <#assignmybeanreq=request.mybeanreq>
  19. <p>page:${mybean.string}
  20. <#listmybean.arrayasitem>
  21. <br>${item}
  22. </#list>
  23. <br>request:${mybeanreq.string}
  24. <p><b>Note:</b>Startingfrom<SPANclass=hilite1>FreeMarker</SPAN>2.2youcanusecustomJSPtagsin
  25. <SPANclass=hilite1>FreeMarker</SPAN>templates.IfyouwanttomigratefromJSPtoFTL(i.e.<SPANclass=hilite1>FreeMarker</SPAN>templates),
  26. thenthat'sprobablyabetteroptionthanembeddingFTLintoJSPpages.
  27. </body>
  28. </html>
  29. </fm:template>
  1. <%@tagliburi="/WEB-INF/fmtag.tld"prefix="fm"%>
  2. <jsp:useBeanid="mybean"class="<spanclass="hilite1">freemarker</span>.examples.jsp.SimpleBean"/>
  3. <jsp:useBeanid="mybeanreq"class="<spanclass="hilite1">freemarker</span>.examples.jsp.SimpleBean"scope="request"/>
  4. <fm:template>
  5. <html>
  6. <head>
  7. <title><spanclass="hilite1">FreeMarker</span>JSPExample</title>
  8. </head>
  9. <body>
  10. <h1><spanclass="hilite1">FreeMarker</span>JSPexample</h1>
  11. <hr>
  12. <p>
  13. ThispageisaJSPpage,yetmostofitscontentsisgeneratedusing
  14. a<spanclass="hilite1">FreeMarker</span>template.Thebelowlinesaretheoutputofcalling
  15. propertiesonaJSP-declaredbeanfromthe<spanclass="hilite1">FreeMarker</span>template:
  16. </p>
  17. <#assignmybean=page.mybean>
  18. <#assignmybeanreq=request.mybeanreq>
  19. <p>page:${mybean.string}
  20. <#listmybean.arrayasitem>
  21. <br>${item}
  22. </#list>
  23. <br>request:${mybeanreq.string}
  24. <p><b>Note:</b>Startingfrom<spanclass="hilite1">FreeMarker</span>2.2youcanusecustomJSPtagsin
  25. <spanclass="hilite1">FreeMarker</span>templates.IfyouwanttomigratefromJSPtoFTL(i.e.<spanclass="hilite1">FreeMarker</span>templates),
  26. thenthat'sprobablyabetteroptionthanembeddingFTLintoJSPpages.
  27. </body>
  28. </html>
  29. </fm:template>

fmtag.tld文件:

Java代码 复制代码
  1. <?xmlversion="1.0"encoding="ISO-8859-1"?>
  2. <!DOCTYPEtaglib
  3. PUBLIC"-//SunMicrosystems,Inc.//DTDJSPTagLibrary1.1//EN"
  4. "http://java.sun.com/j2ee/dtds/web-jsptaglib_1_1.dtd">
  5. <taglib>
  6. <tlibversion>2.0</tlibversion>
  7. <jspversion>1.1</jspversion>
  8. <shortname><SPANclass=hilite1>FreeMarker</SPAN>JSPSupport</shortname>
  9. <tag>
  10. <name>template</name>
  11. <tagclass><SPANclass=hilite1>freemarker</SPAN>.ext.jsp.<SPANclass=hilite1>Freemarker</SPAN>Tag</tagclass>
  12. <bodycontent>tagdependent</bodycontent>
  13. <info>Allowsevaluationof<SPANclass=hilite1>FreeMarker</SPAN>templatesinsideJSP</info>
  14. <attribute>
  15. <name>caching</name>
  16. <required>false</required>
  17. </attribute>
  18. </tag>
  19. </taglib>
  1. <?xmlversion="1.0"encoding="ISO-8859-1"?>
  2. <!DOCTYPEtaglib
  3. PUBLIC"-//SunMicrosystems,Inc.//DTDJSPTagLibrary1.1//EN"
  4. "http://java.sun.com/j2ee/dtds/web-jsptaglib_1_1.dtd">
  5. <taglib>
  6. <tlibversion>2.0</tlibversion>
  7. <jspversion>1.1</jspversion>
  8. <shortname><spanclass="hilite1">FreeMarker</span>JSPSupport</shortname>
  9. <tag>
  10. <name>template</name>
  11. <tagclass><spanclass="hilite1">freemarker</span>.ext.jsp.<spanclass="hilite1">Freemarker</span>Tag</tagclass>
  12. <bodycontent>tagdependent</bodycontent>
  13. <info>Allowsevaluationof<spanclass="hilite1">FreeMarker</span>templatesinsideJSP</info>
  14. <attribute>
  15. <name>caching</name>
  16. <required>false</required>
  17. </attribute>
  18. </tag>
  19. </taglib>


例子3:
inedex.ftl 代码:

Java代码 复制代码
  1. <#import"/lib/common.ftl"ascom>
  2. <#escapexasx?html>
  3. <@com.pagetitle="Index">
  4. <ahref="form.do">Addnewmessage</a>|<ahref="help.html">Help</a>
  5. <#ifguestbook?size=0>
  6. <p>Nomessages.
  7. <#else>
  8. <p>Themessagesare:
  9. <tableborder=0cellspacing=2cellpadding=2width="100%">
  10. <tralign=centervalign=top>
  11. <thbgcolor="#C0C0C0">Name
  12. <thbgcolor="#C0C0C0">Message
  13. <#listguestbookase>
  14. <tralign=leftvalign=top>
  15. <tdbgcolor="#E0E0E0">${e.name}<#ife.email?length!=0>(<ahref="mailto:${e.email}">${e.email}</a>)</#if>
  16. <tdbgcolor="#E0E0E0">${e.message}
  17. </#list>
  18. </table>
  19. </#if>
  20. </@com.page>
  21. </#escape>
  1. <#import"/lib/common.ftl"ascom>
  2. <#escapexasx?html>
  3. <@com.pagetitle="Index">
  4. <ahref="form.do">Addnewmessage</a>|<ahref="help.html">Help</a>
  5. <#ifguestbook?size=0>
  6. <p>Nomessages.
  7. <#else>
  8. <p>Themessagesare:
  9. <tableborder=0cellspacing=2cellpadding=2width="100%">
  10. <tralign=centervalign=top>
  11. <thbgcolor="#C0C0C0">Name
  12. <thbgcolor="#C0C0C0">Message
  13. <#listguestbookase>
  14. <tralign=leftvalign=top>
  15. <tdbgcolor="#E0E0E0">${e.name}<#ife.email?length!=0>(<ahref="mailto:${e.email}">${e.email}</a>)</#if>
  16. <tdbgcolor="#E0E0E0">${e.message}
  17. </#list>
  18. </table>
  19. </#if>
  20. </@com.page>
  21. </#escape>


form.ftl代码
Java代码 复制代码
  1. <#import"/lib/common.ftl"ascom>
  2. <#globalhtml=JspTaglibs["/WEB-INF/struts-html.tld"]>
  3. <#escapexasx?html>
  4. <@com.pagetitle="AddEntry">
  5. <@html.errors/>
  6. <@html.formaction="/add">
  7. <p>Yourname:<br>
  8. <@html.textproperty="name"size="60"/>
  9. <p>Youre-mail(optional):<br>
  10. <@html.textproperty="email"size="60"/>
  11. <p>Message:<br>
  12. <@html.textareaproperty="message"rows="3"cols="60"/>
  13. <p><@html.submitvalue="Submit"/>
  14. </@html.form>
  15. <p><ahref="index.do">Backtotheindexpage</a>
  16. </@com.page>
  17. </#escape>
  1. <#import"/lib/common.ftl"ascom>
  2. <#globalhtml=JspTaglibs["/WEB-INF/struts-html.tld"]>
  3. <#escapexasx?html>
  4. <@com.pagetitle="AddEntry">
  5. <@html.errors/>
  6. <@html.formaction="/add">
  7. <p>Yourname:<br>
  8. <@html.textproperty="name"size="60"/>
  9. <p>Youre-mail(optional):<br>
  10. <@html.textproperty="email"size="60"/>
  11. <p>Message:<br>
  12. <@html.textareaproperty="message"rows="3"cols="60"/>
  13. <p><@html.submitvalue="Submit"/>
  14. </@html.form>
  15. <p><ahref="index.do">Backtotheindexpage</a>
  16. </@com.page>
  17. </#escape>

add.ftl代码内容
Java代码 复制代码
  1. <#import"/lib/common.ftl"ascom>
  2. <#escapexasx?html>
  3. <@com.pagetitle="Entryadded">
  4. <p>Youhaveaddedthefollowingentrytotheguestbook:
  5. <p><b>Name:</b>${guestbookEntry.name}
  6. <#ifguestbookEntry.email?length!=0>
  7. <p><b>Email:</b>${guestbookEntry.email}
  8. </#if>
  9. <p><b>Message:</b>${guestbookEntry.message}
  10. <p><ahref="index.do">Backtotheindexpage...</a>
  11. </@com.page>
  12. </#escape>
  1. <#import"/lib/common.ftl"ascom>
  2. <#escapexasx?html>
  3. <@com.pagetitle="Entryadded">
  4. <p>Youhaveaddedthefollowingentrytotheguestbook:
  5. <p><b>Name:</b>${guestbookEntry.name}
  6. <#ifguestbookEntry.email?length!=0>
  7. <p><b>Email:</b>${guestbookEntry.email}
  8. </#if>
  9. <p><b>Message:</b>${guestbookEntry.message}
  10. <p><ahref="index.do">Backtotheindexpage...</a>
  11. </@com.page>
  12. </#escape>

common.ftl的 代码
Java代码 复制代码
  1. <#macropagetitle>
  2. <html>
  3. <head>
  4. <title><SPANclass=hilite1>FreeMarker</SPAN>StrutsExample-${title?html}</title>
  5. <metahttp-equiv="Content-type"content="text/html;charset=ISO-8859-1">
  6. </head>
  7. <body>
  8. <h1>${title?html}</h1>
  9. <hr>
  10. <#nested>
  11. <hr>
  12. <tableborder="0"cellspacing=0cellpadding=0width="100%">
  13. <trvalign="middle">
  14. <tdalign="left">
  15. <i><SPANclass=hilite1>FreeMarker</SPAN>StrutsExample</i>
  16. <tdalign="right">
  17. <ahref="http://<SPANclass=hilite1>freemarker</SPAN>.org"><imgsrc="poweredby_ffffff.png"border=0></a>
  18. </table>
  19. </body>
  20. </html>
  21. </#macro>
  1. <#macropagetitle>
  2. <html>
  3. <head>
  4. <title><spanclass="hilite1">FreeMarker</span>StrutsExample-${title?html}</title>
  5. <metahttp-equiv="Content-type"content="text/html;charset=ISO-8859-1">
  6. </head>
  7. <body>
  8. <h1>${title?html}</h1>
  9. <hr>
  10. <#nested>
  11. <hr>
  12. <tableborder="0"cellspacing=0cellpadding=0width="100%">
  13. <trvalign="middle">
  14. <tdalign="left">
  15. <i><spanclass="hilite1">FreeMarker</span>StrutsExample</i>
  16. <tdalign="right">
  17. <ahref="http://<spanclass="hilite1">freemarker</span>.org"><imgsrc="poweredby_ffffff.png"border=0></a>
  18. </table>
  19. </body>
  20. </html>
  21. </#macro>
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP