﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Terry.B.Li           彬-文章分类-模板</title><link>http://www.blogjava.net/libin2722/category/34664.html</link><description>虚其心，可解天下之问；专其心，可治天下之学；静其心，可悟天下之理；恒其心，可成天下之业。</description><language>zh-cn</language><lastBuildDate>Fri, 13 Aug 2010 06:55:10 GMT</lastBuildDate><pubDate>Fri, 13 Aug 2010 06:55:10 GMT</pubDate><ttl>60</ttl><item><title>Freemarker中使用OSCache</title><link>http://www.blogjava.net/libin2722/articles/328205.html</link><dc:creator>礼物</dc:creator><author>礼物</author><pubDate>Sat, 07 Aug 2010 07:53:00 GMT</pubDate><guid>http://www.blogjava.net/libin2722/articles/328205.html</guid><wfw:comment>http://www.blogjava.net/libin2722/comments/328205.html</wfw:comment><comments>http://www.blogjava.net/libin2722/articles/328205.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/libin2722/comments/commentRss/328205.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/libin2722/services/trackbacks/328205.html</trackback:ping><description><![CDATA[可以使用maven加入oscache依赖，然后在需要使用oscache的freemarker页面上引入标签&lt;#assign oscache=JspTaglibs['http://www.opensymphony.com/oscache']/&gt;，中括号中可以使用路径方式，然后再页面上需要加入oscache的地方使用&lt;@oscache:标记<br /><br />例如：<br />&lt;#assign oscache=JspTaglibs['http://www.opensymphony.com/oscache']/&gt;<br /><br />&lt;#assign fowUrlaa=""&gt;
<br />&lt;#if RequestParameters.fowUrl?exists&gt;
<br />	&lt;#assign fowUrlaa="${RequestParameters.fowUrl}"&gt;
<br />&lt;/#if&gt;
<br />&lt;@oscache.cache key="__oscache_categories" time=300&gt; <br />    ${fowUrlaa}
<br />&lt;/@oscache.cache&gt;<br /><br />然后再URL上添加参数测试：?fowUrl=12fas11
<img src ="http://www.blogjava.net/libin2722/aggbug/328205.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/libin2722/" target="_blank">礼物</a> 2010-08-07 15:53 <a href="http://www.blogjava.net/libin2722/articles/328205.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>FreeMarker设计指南</title><link>http://www.blogjava.net/libin2722/articles/252665.html</link><dc:creator>礼物</dc:creator><author>礼物</author><pubDate>Wed, 28 Jan 2009 15:20:00 GMT</pubDate><guid>http://www.blogjava.net/libin2722/articles/252665.html</guid><wfw:comment>http://www.blogjava.net/libin2722/comments/252665.html</wfw:comment><comments>http://www.blogjava.net/libin2722/articles/252665.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/libin2722/comments/commentRss/252665.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/libin2722/services/trackbacks/252665.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 1、快速入门（1）模板&#160;+&#160;数据模型&#160;=&#160;输出&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;FreeMarker基于设计者和程序员是具有不同专业技能的不同个体的观念&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;他们是分工劳动...&nbsp;&nbsp;<a href='http://www.blogjava.net/libin2722/articles/252665.html'>阅读全文</a><img src ="http://www.blogjava.net/libin2722/aggbug/252665.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/libin2722/" target="_blank">礼物</a> 2009-01-28 23:20 <a href="http://www.blogjava.net/libin2722/articles/252665.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用freemarker生产静态页面</title><link>http://www.blogjava.net/libin2722/articles/252664.html</link><dc:creator>礼物</dc:creator><author>礼物</author><pubDate>Wed, 28 Jan 2009 15:17:00 GMT</pubDate><guid>http://www.blogjava.net/libin2722/articles/252664.html</guid><wfw:comment>http://www.blogjava.net/libin2722/comments/252664.html</wfw:comment><comments>http://www.blogjava.net/libin2722/articles/252664.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/libin2722/comments/commentRss/252664.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/libin2722/services/trackbacks/252664.html</trackback:ping><description><![CDATA[<span  style="font-family: Verdana; font-size: 14px; ">
<h2 style="margin-right: 0px; margin-bottom: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; font-size: 16.8px; font-weight: bold; margin-top: 8px; margin-left: 8px; text-align: left; ">FreeMarker概述</h2>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">FreeMarker是一个模板引擎，一个基于模板生成文本输出的通用工具，使用纯Java编写</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Template + data model = output</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><img width="416" height="191" src="http://www.ntsky.com/UserFiles/Image/2007/10-20/overview.png" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
    <h2 style="margin-right: 0px; margin-bottom: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; font-size: 16.8px; font-weight: bold; margin-top: 8px; margin-left: 8px; text-align: left; ">Hello world</h2>
    </li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">FreeMarkerTest.java</li>
</ul>
<pre class="java">&#160;<br />
<span style="color: #a1a100; ">import java.io.StringWriter;</span><span style="color: #a1a100; ">import java.util.HashMap;</span><span style="color: #a1a100; ">import java.util.Locale;</span><br />
&#160;<br />
<span style="color: #a1a100; ">import freemarker.template.Configuration;</span><span style="color: #a1a100; ">import freemarker.template.Template;</span><br />
&#160;<br />
<span style="font-weight: bold; color: #000000; ">public</span><span style="font-weight: bold; color: #000000; ">class</span> FreeMarkerTest <span style="color: #66cc66; ">{</span><br />
&#160;<br />
<span style="font-weight: bold; color: #000000; ">public</span><span style="font-weight: bold; color: #000000; ">static</span><span style="color: #993333; ">void</span> main<span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">String</span><span style="color: #66cc66; ">[</span><span style="color: #66cc66; ">]</span> args<span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">{</span><br />
FreeMarkerTest test = <span style="font-weight: bold; color: #000000; ">new</span> FreeMarkerTest<span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
test.<span style="color: #006600; ">sayHello</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"Hermit"</span><span style="color: #66cc66; ">)</span>;<br />
<span style="color: #66cc66; ">}</span><br />
&#160;<br />
<span style="font-weight: bold; color: #000000; ">public</span><span style="color: #993333; ">void</span> sayHello<span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">String</span> name<span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">{</span><br />
Configuration freemarkerCfg = <span style="font-weight: bold; color: #000000; ">new</span> Configuration<span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
freemarkerCfg.<span style="color: #006600; ">setClassForTemplateLoading</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #000000; ">this</span>.<span style="color: #006600; ">getClass</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>, <span style="color: #ff0000; ">"/"</span><span style="color: #66cc66; ">)</span>;<br />
&#160;<br />
freemarkerCfg.<span style="color: #006600; ">setEncoding</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">getDefault</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>, <span style="color: #ff0000; ">"UTF-8"</span><span style="color: #66cc66; ">)</span>;<br />
Template template;<br />
<span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">setDefault</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">ENGLISH</span><span style="color: #66cc66; ">)</span>;<br />
<span style="font-weight: bold; color: #000000; ">try</span><span style="color: #66cc66; ">{</span><br />
template = freemarkerCfg.<span style="color: #006600; ">getTemplate</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"Hello.ftl"</span><span style="color: #66cc66; ">)</span>;<br />
template.<span style="color: #006600; ">setEncoding</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"UTF-8"</span><span style="color: #66cc66; ">)</span>;<br />
<span style="font-weight: bold; color: #aaaadd; ">HashMap</span> root = <span style="font-weight: bold; color: #000000; ">new</span><span style="font-weight: bold; color: #aaaadd; ">HashMap</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
root.<span style="color: #006600; ">put</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"user"</span>, name<span style="color: #66cc66; ">)</span>;<br />
&#160;<br />
<span style="font-weight: bold; color: #aaaadd; ">StringWriter</span> writer = <span style="font-weight: bold; color: #000000; ">new</span><span style="font-weight: bold; color: #aaaadd; ">StringWriter</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
template.<span style="color: #006600; ">process</span><span style="color: #66cc66; ">(</span>root, writer<span style="color: #66cc66; ">)</span>;<br />
<span style="font-weight: bold; color: #aaaadd; ">System</span>.<span style="color: #006600; ">out</span>.<span style="color: #006600; ">println</span><span style="color: #66cc66; ">(</span>writer.<span style="color: #006600; ">toString</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">)</span>;<br />
<span style="color: #66cc66; ">}</span><span style="font-weight: bold; color: #000000; ">catch</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">Exception</span> e<span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">{</span><br />
e.<span style="color: #006600; ">printStackTrace</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
<span style="color: #66cc66; ">}</span><span style="color: #66cc66; ">}</span><br />
&#160;<br />
&#160;<br />
&#160;<br />
<span style="color: #66cc66; ">}</span><br />
&#160;</pre>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Hello.ftl</li>
</ul>
<pre class="html4strict">&#160;<br />
&#160;<br />
Hello ${user}!<br />
&#160;<br />
&#160;</pre>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">输出</li>
</ul>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Hello Hermit!</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><br />
</p>
<a name=".E7.A9.BA.E5.80.BC.E7.9A.84.E5.A4.84.E7.90.86" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); "></a>
<h2 style="margin-right: 0px; margin-bottom: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; font-size: 16.8px; font-weight: bold; margin-top: 8px; margin-left: 8px; text-align: left; ">空值的处理</h2>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">在我们的程序中难免会碰到值为空的时候，如果用一个空值直接去替换模板中的标记，freemarker会毫不犹豫的抛出异常，并把错误信息直接写到输出结果里。为了对付这种情况我们有两种写法</p>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">模板</li>
</ul>
<pre class="html4strict">&#160;<br />
Hello ${user!}!<br />
Hello ${user?if_exists}<br />
Hello ${user!'your name'}!<br />
Hello ${user?default('your name')}<br />
&#160;</pre>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">程序</li>
</ul>
<pre class="java">&#160;<br />
test.<span style="color: #006600; ">sayHello</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #000000; ">null</span><span style="color: #66cc66; ">)</span>;<br />
&#160;</pre>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">输出</li>
</ul>
<pre class="java">&#160;<br />
Hello&#160;!<br />
Hello your name!<br />
Hello <br />
Hello your name<br />
&#160;</pre>
<a name="freemarker.E5.9B.BD.E9.99.85.E5.8C.96.E6.A8.A1.E6.9D.BF" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); "></a>
<h2 style="margin-right: 0px; margin-bottom: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; font-size: 16.8px; font-weight: bold; margin-top: 8px; margin-left: 8px; text-align: left; ">freemarker国际化模板</h2>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">freemarker支持多语言国际化，只要把模板名称按照java资源文件的写法就可以了,也就是name_语言_国家地区.ftl 如果找不到对应的语言，就会用默认语言的模板。</p>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">程序</li>
</ul>
<pre class="java">&#160;<br />
<span style="color: #a1a100; ">import java.io.StringWriter;</span><span style="color: #a1a100; ">import java.util.HashMap;</span><span style="color: #a1a100; ">import java.util.Locale;</span><br />
&#160;<br />
<span style="color: #a1a100; ">import freemarker.template.Configuration;</span><span style="color: #a1a100; ">import freemarker.template.Template;</span><br />
&#160;<br />
<span style="font-weight: bold; color: #000000; ">public</span><span style="font-weight: bold; color: #000000; ">class</span> FreeMarkerTest <span style="color: #66cc66; ">{</span><br />
&#160;<br />
<span style="font-weight: bold; color: #000000; ">public</span><span style="font-weight: bold; color: #000000; ">static</span><span style="color: #993333; ">void</span> main<span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">String</span><span style="color: #66cc66; ">[</span><span style="color: #66cc66; ">]</span> args<span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">{</span><br />
FreeMarkerTest test = <span style="font-weight: bold; color: #000000; ">new</span> FreeMarkerTest<span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
test.<span style="color: #006600; ">sayHello</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"hermit"</span>,<span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">CHINA</span><span style="color: #66cc66; ">)</span>;<br />
test.<span style="color: #006600; ">sayHello</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"hermit"</span>,<span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">ENGLISH</span><span style="color: #66cc66; ">)</span>;<br />
<span style="color: #66cc66; ">}</span><br />
&#160;<br />
<span style="font-weight: bold; color: #000000; ">public</span><span style="color: #993333; ">void</span> sayHello<span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">String</span> name,<span style="font-weight: bold; color: #aaaadd; ">Locale</span> locale<span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">{</span><br />
Configuration freemarkerCfg = <span style="font-weight: bold; color: #000000; ">new</span> Configuration<span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
freemarkerCfg.<span style="color: #006600; ">setClassForTemplateLoading</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #000000; ">this</span>.<span style="color: #006600; ">getClass</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>, <span style="color: #ff0000; ">"/"</span><span style="color: #66cc66; ">)</span>;<br />
&#160;<br />
freemarkerCfg.<span style="color: #006600; ">setEncoding</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">getDefault</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>, <span style="color: #ff0000; ">"UTF-8"</span><span style="color: #66cc66; ">)</span>;<br />
Template template;<br />
<span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">setDefault</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">ENGLISH</span><span style="color: #66cc66; ">)</span>;<br />
<span style="font-weight: bold; color: #000000; ">try</span><span style="color: #66cc66; ">{</span><br />
template = freemarkerCfg.<span style="color: #006600; ">getTemplate</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"Hello.ftl"</span>,locale<span style="color: #66cc66; ">)</span>;<br />
template.<span style="color: #006600; ">setEncoding</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"UTF-8"</span><span style="color: #66cc66; ">)</span>;<br />
<span style="font-weight: bold; color: #aaaadd; ">HashMap</span> root = <span style="font-weight: bold; color: #000000; ">new</span><span style="font-weight: bold; color: #aaaadd; ">HashMap</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
root.<span style="color: #006600; ">put</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"user"</span>, name<span style="color: #66cc66; ">)</span>;<br />
&#160;<br />
<span style="font-weight: bold; color: #aaaadd; ">StringWriter</span> writer = <span style="font-weight: bold; color: #000000; ">new</span><span style="font-weight: bold; color: #aaaadd; ">StringWriter</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
template.<span style="color: #006600; ">process</span><span style="color: #66cc66; ">(</span>root, writer<span style="color: #66cc66; ">)</span>;<br />
<span style="font-weight: bold; color: #aaaadd; ">System</span>.<span style="color: #006600; ">out</span>.<span style="color: #006600; ">println</span><span style="color: #66cc66; ">(</span>writer.<span style="color: #006600; ">toString</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">)</span>;<br />
<span style="color: #66cc66; ">}</span><span style="font-weight: bold; color: #000000; ">catch</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">Exception</span> e<span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">{</span><br />
e.<span style="color: #006600; ">printStackTrace</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
<span style="color: #66cc66; ">}</span><span style="color: #66cc66; "><br />
}</span><br />
&#160;&#160;<br />
<span style="color: #66cc66; ">}</span><br />
&#160;</pre>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">默认语言模版:Hello.ftl<br />
    </li>
</ul>
<pre class="html4strict">Hello ${user!}!&#160;<br />
中文模版:Hello_zh_CN.ftl <br />
你好 ${user!}!<br />
<br />
输出<br />
</pre>
<pre class="html4strict">你好 hermit!<br />
Hello hermit!<br />
&#160;</pre>
<a name=".E5.9C.A8struts.E9.A1.B9.E7.9B.AE.E4.B8.AD.E4.BD.BF.E7.94.A8freemarker" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); "></a>
<h2 style="margin-right: 0px; margin-bottom: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; font-size: 16.8px; font-weight: bold; margin-top: 8px; margin-left: 8px; text-align: left; ">在struts项目中使用freemarker</h2>
<dl><dd style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">1、引入freemarker.jar</dd><dd style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">2、web.xml加入<br />
</dd></dl>
<pre class="xml">	<span style="color: #009900; "><span style="color: #808080; font-style: italic; ">&lt;!-- FreeMarker view servlet (to replace JSP) --></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; "><br />
&lt;servlet<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;servlet-name<span style="font-weight: bold; color: black; ">></span></span></span>freemarker<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/servlet-name<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;servlet-class<span style="font-weight: bold; color: black; ">></span></span></span><br />
freemarker.ext.servlet.FreemarkerServlet<br />
<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/servlet-class<span style="font-weight: bold; color: black; ">></span></span></span><br />
&#160;<br />
<span style="color: #009900; "><span style="color: #808080; font-style: italic; ">&lt;!-- FreemarkerServlet settings: --></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; "><br />
&lt;init-param<span style="font-weight: bold; color: black; ">><br />
</span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-name<span style="font-weight: bold; color: black; ">></span></span></span>TemplatePath<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-name<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; "><br />
&lt;param-value<span style="font-weight: bold; color: black; ">></span></span></span>/<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-value<span style="font-weight: bold; color: black; ">><br />
</span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/init-param<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; "><br />
&lt;init-param<span style="font-weight: bold; color: black; ">><br />
</span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-name<span style="font-weight: bold; color: black; ">></span></span></span>NoCache<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-name<span style="font-weight: bold; color: black; ">><br />
</span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-value<span style="font-weight: bold; color: black; ">></span></span></span>true<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-value<span style="font-weight: bold; color: black; ">><br />
</span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/init-param<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; "><br />
&lt;init-param<span style="font-weight: bold; color: black; ">><br />
</span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-name<span style="font-weight: bold; color: black; ">></span></span></span>ContentType<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-name<span style="font-weight: bold; color: black; ">><br />
</span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-value<span style="font-weight: bold; color: black; ">></span></span></span>text/html<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-value<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; "><br />
&lt;/init-param<span style="font-weight: bold; color: black; ">></span></span></span><br />
&#160;<br />
<span style="color: #009900; "><span style="color: #808080; font-style: italic; ">&lt;!-- FreeMarker settings: --></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;init-param<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-name<span style="font-weight: bold; color: black; ">></span></span></span>template_update_delay<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-name<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-value<span style="font-weight: bold; color: black; ">></span></span></span>0<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-value<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="color: #808080; font-style: italic; "><br />
&lt;!-- 0 is for development only! Use higher value otherwise. --></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; "><br />
&lt;/init-param<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;init-param<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-name<span style="font-weight: bold; color: black; ">></span></span></span>default_encoding<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-name<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; "><br />
&lt;param-value<span style="font-weight: bold; color: black; ">></span></span></span>utf-8<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-value<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/init-param<span style="font-weight: bold; color: black; ">><br />
</span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;init-param<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-name<span style="font-weight: bold; color: black; ">></span></span></span>locale<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-name<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-value<span style="font-weight: bold; color: black; ">></span></span></span>en_US<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-value<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/init-param<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; "><br />
&lt;init-param<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-name<span style="font-weight: bold; color: black; ">></span></span></span>number_format<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-name<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;param-value<span style="font-weight: bold; color: black; ">></span></span></span>0.##########<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/param-value<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/init-param<span style="font-weight: bold; color: black; ">></span></span></span><br />
&#160;<br />
<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;load-on-startup<span style="font-weight: bold; color: black; ">></span></span></span>1<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/load-on-startup<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/servlet<span style="font-weight: bold; color: black; ">></span></span></span><br />
&#160;<br />
<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;servlet-mapping<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;servlet-name<span style="font-weight: bold; color: black; ">></span></span></span>freemarker<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/servlet-name<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;url-pattern<span style="font-weight: bold; color: black; ">></span></span></span>*.ftl<span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/url-pattern<span style="font-weight: bold; color: black; ">></span></span></span><span style="color: #009900; "><span style="font-weight: bold; color: black; ">&lt;/servlet-mapping<span style="font-weight: bold; color: black; ">></span></span></span><br />
&#160;</pre>
<dl><dd style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">3、一个示例页面</dd></dl>
<pre class="html4strict">&#160;<br />
<span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;html></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;head></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;title></span></span>Say Hello<span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;/title></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;META</span><span style="color: #000066; ">HTTP-EQUIV</span>=<span style="color: #ff0000; ">"Content-Type"</span><span style="color: #000066; ">CONTENT</span>=<span style="color: #ff0000; ">"text/html; charset=utf-8"</span><span style="font-weight: bold; color: #000000; ">></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;/head></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;body></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;h1></span></span>Hello ${user}!<span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;/h1></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;/body></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;/html></span></span><br />
&#160;</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">我们完全可以用freemarker的模板取代JSP页面。用freemarker的模板看起更简洁，可读性更强。比如现在struts2的UI标签就是用freemarker做的。</p>
<a name="freemarker.E7.94.A8struts.E6.A0.87.E7.AD.BE.E5.81.9A.E5.9B.BD.E9.99.85.E5.8C.96" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); "></a>
<h2 style="margin-right: 0px; margin-bottom: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; font-size: 16.8px; font-weight: bold; margin-top: 8px; margin-left: 8px; text-align: left; ">freemarker用struts标签做国际化</h2>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">示例模板<br />
    </li>
</ul>
<pre class="html4strict"><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;</span>#assign  html =JspTaglibs<span style="color: #66cc66; ">[</span><span style="color: #ff0000; ">"/WEB-INF/struts-html.tld"</span><span style="color: #66cc66; ">]</span><span style="font-weight: bold; color: #000000; ">></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;</span>#assign  bean =JspTaglibs<span style="color: #66cc66; ">[</span><span style="color: #ff0000; ">"/WEB-INF/struts-bean.tld"</span><span style="color: #66cc66; ">]</span><span style="font-weight: bold; color: #000000; ">></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;</span>#assign  logic =JspTaglibs<span style="color: #66cc66; ">[</span><span style="color: #ff0000; ">"/WEB-INF/struts-logic.tld"</span><span style="color: #66cc66; ">]</span><span style="font-weight: bold; color: #000000; ">></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;html></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;head></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;title></span></span> FreeMarker Struts Example <span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;/title></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;meta</span><span style="color: #000066; ">http-equiv</span> =<span style="color: #ff0000; ">"Content-type"</span><span style="color: #000066; ">content</span> =<span style="color: #ff0000; ">"text/html; charset=utf-8"</span><span style="font-weight: bold; color: #000000; ">></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;</span>/ head <span style="font-weight: bold; color: #000000; ">></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;body></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;</span>@bean.message key =<span style="color: #ff0000; ">"hello"</span>  arg0 =<span style="color: #ff0000; ">"hermit"</span>/<span style="font-weight: bold; color: #000000; ">></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;/body></span></span><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;/html></span></span> </pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">主要是引入标签的时候要这样写：<br />
</p>
<pre class="html4strict"><span style="color: #009900; "><span style="font-weight: bold; color: #000000; ">&lt;</span>#assign  html =JspTaglibs<span style="color: #66cc66; ">[</span><span style="color: #ff0000; ">"/WEB-INF/struts-html.tld"</span><span style="color: #66cc66; ">]</span><span style="font-weight: bold; color: #000000; ">></span></span> </pre>
<a name="freemarker.E7.9B.B4.E6.8E.A5.E4.BD.BF.E7.94.A8.E8.B5.84.E6.BA.90.E6.96.87.E4.BB.B6.E8.BF.9B.E8.A1.8C.E5.A4.9A.E8.AF.AD.E8.A8.80.E5.9B.BD.E9.99.85.E5.8C.96" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); "></a>
<h2 style="margin-right: 0px; margin-bottom: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; font-size: 16.8px; font-weight: bold; margin-top: 8px; margin-left: 8px; text-align: left; ">freemarker直接使用资源文件进行多语言国际化</h2>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">程序<br />
    </li>
</ul>
<pre class="java"><span style="color: #a1a100; ">import java.io.StringWriter;</span><span style="color: #a1a100; ">import java.util.HashMap;</span><span style="color: #a1a100; ">import java.util.Locale;</span><span style="color: #a1a100; ">import java.util.ResourceBundle;</span><br />
&#160;<br />
<span style="color: #a1a100; ">import freemarker.ext.beans.BeansWrapper;</span><span style="color: #a1a100; ">import freemarker.ext.beans.ResourceBundleModel;</span><span style="color: #a1a100; ">import freemarker.template.Configuration;</span><span style="color: #a1a100; ">import freemarker.template.Template;</span><br />
&#160;<br />
<span style="font-weight: bold; color: #000000; ">public</span><span style="font-weight: bold; color: #000000; ">class</span> FreeMarkerTest <span style="color: #66cc66; ">{</span><br />
&#160;<br />
<span style="font-weight: bold; color: #000000; ">public</span><span style="font-weight: bold; color: #000000; ">static</span><span style="color: #993333; ">void</span> main<span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">String</span><span style="color: #66cc66; ">[</span><span style="color: #66cc66; ">]</span> args<span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">{</span><br />
FreeMarkerTest test = <span style="font-weight: bold; color: #000000; ">new</span> FreeMarkerTest<span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
test.<span style="color: #006600; ">sayHello</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"hermit"</span>,<span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">CHINA</span><span style="color: #66cc66; ">)</span>;<br />
test.<span style="color: #006600; ">sayHello</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"hermit"</span>,<span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">ENGLISH</span><span style="color: #66cc66; ">)</span>;<br />
<span style="color: #66cc66; ">}</span><br />
&#160;<br />
<span style="font-weight: bold; color: #000000; ">public</span><span style="color: #993333; ">void</span> sayHello<span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">String</span> name,<span style="font-weight: bold; color: #aaaadd; ">Locale</span> locale<span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">{</span><br />
Configuration freemarkerCfg = <span style="font-weight: bold; color: #000000; ">new</span> Configuration<span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
freemarkerCfg.<span style="color: #006600; ">setClassForTemplateLoading</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #000000; ">this</span>.<span style="color: #006600; ">getClass</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>, <span style="color: #ff0000; ">"/"</span><span style="color: #66cc66; ">)</span>;<br />
&#160;<br />
freemarkerCfg.<span style="color: #006600; ">setEncoding</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">getDefault</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>, <span style="color: #ff0000; ">"UTF-8"</span><span style="color: #66cc66; ">)</span>;<br />
Template template;<br />
<span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">setDefault</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">Locale</span>.<span style="color: #006600; ">ENGLISH</span><span style="color: #66cc66; ">)</span>;<br />
<span style="font-weight: bold; color: #000000; ">try</span><span style="color: #66cc66; ">{</span><br />
template = freemarkerCfg.<span style="color: #006600; ">getTemplate</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"Hello.ftl"</span><span style="color: #66cc66; ">)</span>;<br />
template.<span style="color: #006600; ">setEncoding</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"UTF-8"</span><span style="color: #66cc66; ">)</span>;<br />
<span style="font-weight: bold; color: #aaaadd; ">HashMap</span> root = <span style="font-weight: bold; color: #000000; ">new</span><span style="font-weight: bold; color: #aaaadd; ">HashMap</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
root.<span style="color: #006600; ">put</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"user"</span>, name<span style="color: #66cc66; ">)</span>;<br />
<span style="font-weight: bold; color: #aaaadd; ">ResourceBundle</span> RESOURCE_BUNDLE = <span style="font-weight: bold; color: #aaaadd; ">ResourceBundle</span>.<span style="color: #006600; ">getBundle</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"ApplicationResources"</span>,locale<span style="color: #66cc66; ">)</span>;<br />
ResourceBundleModel rsbm = <span style="font-weight: bold; color: #000000; ">new</span> ResourceBundleModel<span style="color: #66cc66; ">(</span>RESOURCE_BUNDLE,<span style="font-weight: bold; color: #000000; ">new</span> BeansWrapper<span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">)</span>;<br />
root.<span style="color: #006600; ">put</span><span style="color: #66cc66; ">(</span><span style="color: #ff0000; ">"bundle"</span>, rsbm<span style="color: #66cc66; ">)</span>;<br />
<span style="font-weight: bold; color: #aaaadd; ">StringWriter</span> writer = <span style="font-weight: bold; color: #000000; ">new</span><span style="font-weight: bold; color: #aaaadd; ">StringWriter</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
template.<span style="color: #006600; ">process</span><span style="color: #66cc66; ">(</span>root, writer<span style="color: #66cc66; ">)</span>;<br />
<span style="font-weight: bold; color: #aaaadd; ">System</span>.<span style="color: #006600; ">out</span>.<span style="color: #006600; ">println</span><span style="color: #66cc66; ">(</span>writer.<span style="color: #006600; ">toString</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">)</span>;<br />
<span style="color: #66cc66; ">}</span><span style="font-weight: bold; color: #000000; ">catch</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #aaaadd; ">Exception</span> e<span style="color: #66cc66; ">)</span><span style="color: #66cc66; ">{</span><br />
e.<span style="color: #006600; ">printStackTrace</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>;<br />
<span style="color: #66cc66; ">}</span><span style="color: #66cc66; ">}</span><br />
&#160;<br />
&#160;<br />
&#160;<br />
<span style="color: #66cc66; ">}</span> <br />
<br />
模板<br />
</pre>
<pre class="html4strict">${bundle("hello","hermit")} <br />
<br />
默认语言资源文件 </pre>
<pre class="html4strict">hello=Hello {0}\! <br />
<br />
中文资源文件 <br />
<br />
hello=你好 {0}\!</pre>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">输出<br />
    </li>
</ul>
<pre class="html4strict">你好 hermit!<br />
Hello hermit!</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">关键的地方就是用ResourceBundleModel把ResourceBundle转换一下。</p>
<a name=".E5.B8.B8.E7.94.A8.E7.9A.842.E7.A7.8D.E5.8A.A0.E8.BD.BD.E6.A8.A1.E6.9D.BF.E7.9A.84.E6.96.B9.E5.BC.8F" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); "></a>
<h2 style="margin-right: 0px; margin-bottom: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; font-size: 16.8px; font-weight: bold; margin-top: 8px; margin-left: 8px; text-align: left; ">常用的2种加载模板的方式</h2>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">普通java类根据当前class上下文环境加载模板<br />
    </li>
</ul>
<pre class="java">cfg.<span style="color: #006600; ">setClassForTemplateLoading</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #000000; ">this</span>.<span style="color: #006600; ">getClass</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>, <span style="color: #ff0000; ">"/"</span><span style="color: #66cc66; ">)</span>;</pre>
<ul>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">在web项目中根据servlet上下文环境加载模板<br />
    </li>
</ul>
<pre class="java">cfg.<span style="color: #006600; ">setServletContextForTemplateLoading</span><span style="color: #66cc66; ">(</span><span style="font-weight: bold; color: #000000; ">this</span>.<span style="color: #006600; ">getServlet</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>.<span style="color: #006600; ">getServletContext</span><span style="color: #66cc66; ">(</span><span style="color: #66cc66; ">)</span>, <span style="color: #ff0000; ">"/"</span><span style="color: #66cc66; ">)</span>;</pre>
</span>
<img src ="http://www.blogjava.net/libin2722/aggbug/252664.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/libin2722/" target="_blank">礼物</a> 2009-01-28 23:17 <a href="http://www.blogjava.net/libin2722/articles/252664.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>我的第一个FreeMarker程序</title><link>http://www.blogjava.net/libin2722/articles/229284.html</link><dc:creator>礼物</dc:creator><author>礼物</author><pubDate>Tue, 16 Sep 2008 16:00:00 GMT</pubDate><guid>http://www.blogjava.net/libin2722/articles/229284.html</guid><wfw:comment>http://www.blogjava.net/libin2722/comments/229284.html</wfw:comment><comments>http://www.blogjava.net/libin2722/articles/229284.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/libin2722/comments/commentRss/229284.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/libin2722/services/trackbacks/229284.html</trackback:ping><description><![CDATA[<p>1、java应用程序中<br />
&nbsp;&nbsp;&nbsp; 我用maven导入FreeMarker的jar包<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; &lt;dependency&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;groupId&gt;freemarker&lt;/groupId&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;artifactId&gt;freemarker&lt;/artifactId&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;version&gt;2.3.8&lt;/version&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;/dependency&gt;</p>
<p>&nbsp;&nbsp;&nbsp; package com.test;</p>
<p>import java.io.IOException;<br />
import java.io.StringWriter;<br />
import java.util.HashMap;<br />
import java.util.Map;</p>
<p><br />
import freemarker.cache.ClassTemplateLoader;<br />
import freemarker.template.Configuration;<br />
import freemarker.template.DefaultObjectWrapper;<br />
import freemarker.template.Template;<br />
import freemarker.template.TemplateException;</p>
<p>public class Test {</p>
<p>&nbsp;&nbsp;&nbsp; /** *//**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param args<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Configuration configuration = new Configuration();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; configuration.setObjectWrapper(new DefaultObjectWrapper());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; configuration.setTemplateLoader(new ClassTemplateLoader(Test.class, "/com/test"));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Template template = configuration.getTemplate("temp.ftl");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringWriter writer = new StringWriter();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Map&lt;String, Object&gt; context = new HashMap&lt;String, Object&gt;();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context.put("message", "我的第一个FreeMarker程序");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; template.process(context, writer);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(writer.toString());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IOException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO Auto-generated catch block<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (TemplateException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO Auto-generated catch block<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp; }</p>
<p>}</p>
<p>这里是用java应用程序，需要加入下面两句（设置模板文件载入）：<br />
方法一：<br />
&nbsp; configuration.setObjectWrapper(new DefaultObjectWrapper());<br />
&nbsp; configuration.setTemplateLoader(new ClassTemplateLoader(Test.class, "/com/test"));</p>
<p>其中"/com/test" 是模板文件所在的文件夹，前面的&#8220;/&#8221;必须，这里我的模板文件和当前java类在同一路径下，则这样写，如果你的模板文件在src跟目录下，这里只需要写&#8220;/&#8221;就可以了</p>
<p>context.put("message", "我的第一个FreeMarker程序");<br />
是模板文件中用到的变量，map中的变量可以是javabean，也可以是对象</p>
<p><br />
方法二：<br />
configuration.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/templates");<br />
这里主要针对Servlet的时候，当然在Servlet中也可以用方法一提到的写法<br />
package com.newegg.lab.freemarker.servlet;</p>
<p>import java.io.IOException;<br />
import java.io.StringWriter;<br />
import java.io.Writer;<br />
import java.util.HashMap;<br />
import java.util.Map;</p>
<p>import javax.servlet.ServletException;<br />
import javax.servlet.http.HttpServletRequest;<br />
import javax.servlet.http.HttpServletResponse;</p>
<p>import freemarker.cache.ClassTemplateLoader;<br />
import freemarker.template.Configuration;<br />
import freemarker.template.DefaultObjectWrapper;<br />
import freemarker.template.Template;<br />
import freemarker.template.TemplateException;</p>
<p>/** *//**<br />
&nbsp;* Servlet implementation class for Servlet: FreemarkerServlet<br />
&nbsp;*<br />
&nbsp;*/<br />
public class FreemarkerServlet extends javax.servlet.http.HttpServlet implements<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; javax.servlet.Servlet {</p>
<p>&nbsp;&nbsp;&nbsp; private Configuration configuration;</p>
<p>&nbsp;&nbsp;&nbsp; private Template template;<br />
&nbsp;&nbsp;&nbsp; /**//*<br />
&nbsp;&nbsp;&nbsp;&nbsp; * (non-Java-doc)<br />
&nbsp;&nbsp;&nbsp;&nbsp; *<br />
&nbsp;&nbsp;&nbsp;&nbsp; * @see javax.servlet.http.HttpServlet#HttpServlet()<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public FreemarkerServlet() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super();<br />
&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp; @Override<br />
&nbsp;&nbsp;&nbsp; public void destroy() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO Auto-generated method stub<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; configuration = null;<br />
&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp; @Override<br />
&nbsp;&nbsp;&nbsp; public void init() throws ServletException {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO Auto-generated method stub<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; configuration = new Configuration();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; configuration.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/templates");<br />
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; configuration.setObjectWrapper(new DefaultObjectWrapper());<br />
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; configuration.setTemplateLoader(new ClassTemplateLoader(FreemarkerServlet.class, "/"));<br />
&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp; /**//*<br />
&nbsp;&nbsp;&nbsp;&nbsp; * (non-Java-doc)<br />
&nbsp;&nbsp;&nbsp;&nbsp; *<br />
&nbsp;&nbsp;&nbsp;&nbsp; * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,<br />
&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpServletResponse response)<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; protected void doGet(HttpServletRequest request,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpServletResponse response) throws ServletException, IOException {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO Auto-generated method stub<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.doPost(request, response);<br />
&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp; /**//*<br />
&nbsp;&nbsp;&nbsp;&nbsp; * (non-Java-doc)<br />
&nbsp;&nbsp;&nbsp;&nbsp; *<br />
&nbsp;&nbsp;&nbsp;&nbsp; * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,<br />
&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpServletResponse response)<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; protected void doPost(HttpServletRequest request,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpServletResponse response) throws ServletException, IOException {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO Auto-generated method stub<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Map root = new HashMap();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root.put("message", "Hello World!的的的的");</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Get the templat object<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Template t = configuration.getTemplate("test.ftl");</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Prepare the HTTP response:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // - Use the charset of template for the output<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // - Use text/html MIME-type<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; response.setContentType("text/html; charset=" + t.getEncoding());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Writer out = response.getWriter();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Merge the data-model and the template<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.process(root, out);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringWriter stringWriter = new StringWriter();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.process(root, stringWriter);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(stringWriter.toString());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (TemplateException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new ServletException(<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Error while processing FreeMarker template", e);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp; }<br />
}</p>
<p><br />
模板文件中需要使用 ${}将java类中在Map对象中声明的变量包围起来就可以了</p>
<p>例如模板文件：<br />
&lt;pre&gt;<br />
//描述信息是 : ${message}<br />
&lt;/pre&gt;</p>
<p><br />
则得到的结果是：<br />
&lt;pre&gt;<br />
//描述信息是 : Hello World!的的的的<br />
&lt;/pre&gt;<br />
</p>
<img src ="http://www.blogjava.net/libin2722/aggbug/229284.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/libin2722/" target="_blank">礼物</a> 2008-09-17 00:00 <a href="http://www.blogjava.net/libin2722/articles/229284.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)FreeMarker中文参考手册</title><link>http://www.blogjava.net/libin2722/articles/229263.html</link><dc:creator>礼物</dc:creator><author>礼物</author><pubDate>Tue, 16 Sep 2008 13:56:00 GMT</pubDate><guid>http://www.blogjava.net/libin2722/articles/229263.html</guid><wfw:comment>http://www.blogjava.net/libin2722/comments/229263.html</wfw:comment><comments>http://www.blogjava.net/libin2722/articles/229263.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/libin2722/comments/commentRss/229263.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/libin2722/services/trackbacks/229263.html</trackback:ping><description><![CDATA[<h3 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-1_E6_A8_A1_E6_9D_BF_E6_95_B0_E6_8D_AE_E6_A8_A1_E5_9E_8B_E8_BE_93_E5_87_BA">（1）模板 + 数据模型 = 输出 </h3>
<p>FreeMarker基于设计者和程序员是具有不同专业技能的不同个体的观念他们是分工劳动的：设计者专注于表示——创建HTML文件、图片、Web页面的其它可视化方面；程序员创建系统，生成设计页面要显示的数据。经常会遇到的问题是：在Web页面（或其它类型的文档）中显示的信息在设计页面时是无效的，是基于动态数据的。在这里，你可以在HTML（或其它要输出的文本）中加入一些特定指令，FreeMarker会在输出页面给最终用户时，用适当的数据替代这些代码。 </p>
<p>下面是一个例子： </p>
<pre>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome!&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Welcome ${user}!&lt;/h1&gt;
&lt;p&gt;Our latest product:
&lt;a href="${latestProduct.url}"&gt;${latestProduct.name}&lt;/a&gt;!
&lt;/body&gt;
&lt;/html&gt;
</pre>
这个例子是在简单的HTML中加入了一些由${&#8230;}包围的特定代码，这些特定代码是FreeMarker的指令，而包含FreeMarker的指令的文件就称为模板（Template）。至于user、latestProduct.url和latestProduct.name来自于数据模型（data model）。数据模型由程序员编程来创建，向模板提供变化的信息，这些信息来自于数据库、文件，甚至于在程序中直接生成。模板设计者不关心数据从那儿来，只知道使用已经建立的数据模型。
<p>下面是一个可能的数据模型： </p>
<pre>(root)
|
+- user = "Big Joe"
|
+- latestProduct
|
+- url = "products/greenmouse.html"
|
+- name = "green mouse"
</pre>
数据模型类似于计算机的文件系统，latestProduct可以看作是目录。
<h3 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-2_E6_95_B0_E6_8D_AE_E6_A8_A1_E5_9E_8B">2、数据模型 </h3>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-1_E5_9F_BA_E7_A1_80">（1）基础 </h4>
<p>在快速入门中介绍了在模板中使用的三种基本对象类型：scalars、hashes 和sequences，其实还可以有其它更多的能力： </p>
<ul>
    <li>scalars：存储单值 </li>
</ul>
<ul>
    <li>hashes：充当其它对象的容器，每个都关联一个唯一的查询名字 </li>
</ul>
<ul>
    <li>sequences：充当其它对象的容器，按次序访问 </li>
</ul>
<ul>
    <li>方法：通过传递的参数进行计算，以新对象返回结果 </li>
</ul>
<ul>
    <li>用户自定义FTL标记：宏和变换器 </li>
</ul>
<p>通常每个变量只具有上述的一种能力，但一个变量可以具有多个上述能力，如下面的例子： </p>
<pre>(root)
|
+- mouse = "Yerri"
|
+- age = 12
|
+- color = "brown"&gt;
</pre>
mouse既是scalars又是hashes，将上面的数据模型合并到下面的模板：
<pre>${mouse}       &lt;#-- use mouse as scalar --&gt;
${mouse.age}   &lt;#-- use mouse as hash --&gt;
${mouse.color} &lt;#-- use mouse as hash --&gt;
</pre>
输出结果是：
<pre>Yerri
12
brown
</pre>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-2Scalar_E5_8F_98_E9_87_8F">（2）Scalar变量 </h4>
<p>Scalar变量存储单值，可以是： </p>
<ul>
    <li>字符串：简单文本，在模板中使用引号（单引号或双引号）括起 </li>
</ul>
<ul>
    <li>数字：在模板中直接使用数字值 </li>
</ul>
<ul>
    <li>日期：存储日期/时间相关的数据，可以是日期、时间或日期-时间（Timestamp）；通常情况，日期值由程序员加到数据模型中，设计者只需要显示它们 </li>
</ul>
<ul>
    <li>布尔值：true或false，通常在&lt;#if &#8230;&gt;标记中使用 </li>
</ul>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-3hashessequences_E5_92_8C_E9_9B_86_E5_90_88">（3）hashes 、sequences和集合 </h4>
<p>有些变量不包含任何可显示的内容，而是作为容器包含其它变量，者有两种类型： </p>
<ul>
    <li>hashes：具有一个唯一的查询名字和它包含的每个变量相关联 </li>
</ul>
<ul>
    <li>sequences：使用数字和它包含的每个变量相关联，索引值从0开始 </li>
</ul>
<p>集合变量通常类似sequences，除非无法访问它的大小和不能使用索引来获得它的子变量；集合可以看作只能由&lt;#list &#8230;&gt;指令使用的受限sequences </p>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-4_E6_96_B9_E6_B3_95">（4）方法 </h4>
<p>方法变量通常是基于给出的参数计算值。 </p>
<p>下面的例子假设程序员已经将方法变量avg放到数据模型中，用来计算数字平均值： </p>
<pre>The average of 3 and 5 is: ${avg(3, 5)}
The average of 6 and 10 and 20 is: ${avg(6, 10, 20)}
The average of the price of python and elephant is:
${avg(animals.python.price, animals.elephant.price)}
</pre>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-5_E5_AE_8F_E5_92_8C_E5_8F_98_E6_8D_A2_E5_99_A8">（5）宏和变换器 </h4>
<p>宏和变换器变量是用户自定义指令（自定义FTL标记），会在后面讲述这些高级特性 </p>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-6_E8_8A_82_E7_82_B9">（6）节点 </h4>
<p>节点变量表示为树型结构中的一个节点，通常在XML处理中使用，会在后面的专门章节中讲 </p>
<h3 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-3_E6_A8_A1_E6_9D_BF">3、模板 </h3>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-1_E6_95_B4_E4_BD_93_E7_BB_93_E6_9E_84">（1）整体结构 </h4>
<p>模板使用FTL（FreeMarker模板语言）编写，是下面各部分的一个组合： </p>
<ul>
    <li>文本：直接输出
    <li>Interpolation：由${和}，或#{和}来限定，计算值替代输出
    <li>FTL标记：FreeMarker指令，和HTML标记类似，名字前加#予以区分，不会输出
    <li>注释：由&lt;#--和--&gt;限定，不会输出 </li>
</ul>
<p>下面是以一个具体模板例子： </p>
<pre>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome!&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;#-- Greet the user with his/her name --&gt;
&lt;h1&gt;Welcome ${user}!&lt;/h1&gt;
&lt;p&gt;We have these animals:
&lt;ul&gt;
&lt;#list animals as being&gt;
&lt;li&gt;${being.name} for ${being.price} Euros
&lt;/#list&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>注意事项： </p>
<ul>
    <li>FTL区分大小写，所以list是正确的FTL指令，而List不是；${name}和${NAME}是不同的 </li>
</ul>
<ul>
    <li>Interpolation只能在文本中使用 </li>
</ul>
<ul>
    <li>FTL标记不能位于另一个FTL标记内部，例如： </li>
</ul>
<pre>&lt;#if &lt;#include 'foo'&gt;='bar'&gt;...&lt;/if&gt;
</pre>
<ul>
    <li>注释可以位于FTL标记和Interpolation内部，如下面的例子： </li>
</ul>
<pre>&lt;h1&gt;Welcome ${user &lt;#-- The name of user --&gt;}!&lt;/h1&gt;
&lt;p&gt;We have these animals:
&lt;ul&gt;
&lt;#list &lt;#-- some comment... --&gt; animals as &lt;#-- again... --&gt; being&gt;
...
</pre>
<ul>
    <li>余的空白字符会在模板输出时移除 </li>
</ul>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-2_E6_8C_87_E4_BB_A4">（2）指令 </h4>
<p>在FreeMarker中，使用FTL标记引用指令。有三种FTL标记，这和HTML标记是类似的： </p>
<ul>
    <li>开始标记：&lt;#directivename parameters&gt; </li>
</ul>
<ul>
    <li>结束标记：&lt;/#directivename&gt; </li>
</ul>
<ul>
    <li>空内容指令标记：&lt;#directivename parameters/&gt; </li>
</ul>
<p>有两种类型的指令：预定义指令和用户定义指令。 </p>
<p>用户定义指令要使用@替换#，如&lt;@mydirective&gt;...&lt;/@mydirective&gt;（会在后面讲述）。 </p>
<p>FTL标记不能够交叉，而应该正确的嵌套，如下面的代码是错误的： </p>
<pre>&lt;ul&gt;
&lt;#list animals as being&gt;
&lt;li&gt;${being.name} for ${being.price} Euros
&lt;#if use = "Big Joe"&gt;
(except for you)
&lt;/#list&gt;
&lt;/#if&gt; &lt;#-- WRONG! --&gt;
&lt;/ul&gt;
</pre>
如果使用不存在的指令，FreeMarker不会使用模板输出，而是产生一个错误消息。
<p>FreeMarker会忽略FTL标记中的空白字符，如下面的例子： </p>
<pre>&lt;#list
animals       as
being
&gt;
${being.name} for ${being.price} Euros
&lt;/#list    &gt;
</pre>
但是，&lt;、&lt;/和指令之间不允许有空白字符。
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-3_E8_A1_A8_E8_BE_BE_E5_BC_8F">（3）表达式 </h4>
<p><strong>直接指定值</strong> </p>
<ul>
    <li>字符串 </li>
</ul>
使用单引号或双引号限定
<p>如果包含特殊字符需要转义，如下面的例子： </p>
<pre>${"It's \"quoted\" and
this is a backslash: \\"}
${'It\'s "quoted" and
this is a backslash: \\'}
</pre>
输出结果是：
<pre>It's "quoted" and
this is a backslash: \
It's "quoted" and
this is a backslash: \
</pre>
下面是支持的转义序列：
<p>&nbsp;</p>
<table class="wikitable" border="1">
    <tbody>
        <tr>
            <th>转义序列 </th>
            <th>含义 </th>
        </tr>
        <tr>
            <td>\" </td>
            <td>双引号(u0022) </td>
        </tr>
        <tr>
            <td>\' </td>
            <td>单引号(u0027) </td>
        </tr>
        <tr>
            <td></td>
            <td>反斜杠(u005C) </td>
        </tr>
        <tr>
            <td>\n </td>
            <td>换行(u000A) </td>
        </tr>
        <tr>
            <td>\r </td>
            <td>Return (u000D) </td>
        </tr>
        <tr>
            <td>\t </td>
            <td>Tab (u0009) </td>
        </tr>
        <tr>
            <td>\b </td>
            <td>Backspace (u0008) </td>
        </tr>
        <tr>
            <td>\f </td>
            <td>Form feed (u000C) </td>
        </tr>
        <tr>
            <td>\l </td>
            <td>&lt; </td>
        </tr>
        <tr>
            <td>\g </td>
            <td>&gt; </td>
        </tr>
        <tr>
            <td>\a </td>
            <td>&amp; </td>
        </tr>
        <tr>
            <td>\{ </td>
            <td>{ </td>
        </tr>
        <tr>
            <td>\xCode </td>
            <td>4位16进制Unicode代码 </td>
        </tr>
    </tbody>
</table>
<p>&nbsp;</p>
<p>有一类特殊的字符串称为raw字符串，被认为是纯文本，其中的\和{等不具有特殊含义，该类字符串在引号前面加r，下面是一个例子： </p>
<pre>${r"${foo}"}
${r"C:\foo\bar"}
</pre>
输出的结果是：
<pre>${foo}
C:\foo\bar
</pre>
<ul>
    <li>数字 </li>
</ul>
<p>直接输入，不需要引号 </p>
<p>精度数字使用&#8220;.&#8221;分隔，不能使用分组符号 </p>
<p>目前版本不支持科学计数法，所以&#8220;1E3&#8221;是错误的 </p>
<p>不能省略小数点前面的0，所以&#8220;.5&#8221;是错误的 </p>
<p>数字8、+8、08和8.00都是相同的 </p>
<ul>
    <li>布尔值 </li>
</ul>
<p>true和false，不使用引号 </p>
<ul>
    <li>序列 </li>
</ul>
<p>由逗号分隔的子变量列表，由方括号限定，下面是一个例子： </p>
<pre>&lt;#list ["winter", "spring", "summer", "autumn"] as x&gt;
${x}
&lt;/#list&gt;
</pre>
输出的结果是：
<pre>winter
spring
summer
autumn
</pre>
列表的项目是表达式，所以可以有下面的例子：
<pre>[2 + 2, [1, 2, 3, 4], "whatnot"]
</pre>
可以使用数字范围定义数字序列，例如2..5等同于[2, 3, 4, 5]，但是更有效率，注意数字范围没有方括号
<p>可以定义反递增的数字范围，如5..2 </p>
<ul>
    <li>散列（hash） </li>
</ul>
由逗号分隔的键/值列表，由大括号限定，键和值之间用冒号分隔，下面是一个例子：
<pre>{"name":"green mouse", "price":150}
</pre>
键和值都是表达式，但是键必须是字符串
<p><strong>获取变量</strong> </p>
<ul>
    <li>顶层变量： ${variable}，变量名只能是字母、数字、下划线、$、@和#的组合，且不能以数字开头 </li>
</ul>
<ul>
    <li>从散列中获取数据 </li>
</ul>
<p>可以使用点语法或方括号语法，假设有下面的数据模型： </p>
<pre>(root)
|
+- book
|   |
|   +- title = "Breeding green mouses"
|   |
|   +- author
|       |
|       +- name = "Julia Smith"
|       |
|       +- info = "Biologist, 1923-1985, Canada"
|
+- test = "title"
</pre>
下面都是等价的：
<pre>book.author.name
book["author"].name
book.author.["name"]
book["author"]["name"]
</pre>
使用点语法，变量名字有顶层变量一样的限制，但方括号语法没有该限制，因为名字是任意表达式的结果
<ul>
    <li>从序列获得数据：和散列的方括号语法语法一样，只是方括号中的表达式值必须是数字；注意：第一个项目的索引是0 </li>
</ul>
<p>序列片断：使用[startIndex..endIndex]语法，从序列中获得序列片断（也是序列）；startIndex和endIndex是结果为数字的表达式 </p>
<ul>
    <li>特殊变量：FreeMarker内定义变量，使用.variablename语法访问 </li>
</ul>
<p><strong>字符串操作 </strong></p>
<ul>
    <li>Interpolation（或连接操作） </li>
</ul>
<p>可以使用${..}（或#{..}）在文本部分插入表达式的值，例如： </p>
<pre>${"Hello ${user}!"}
${"${user}${user}${user}${user}"}
</pre>
可以使用+操作符获得同样的结果
<pre>${"Hello " + user + "!"}
${user + user + user + user}
</pre>
${..}只能用于文本部分，下面的代码是错误的：
<pre>&lt;#if ${isBig}&gt;Wow!&lt;/#if&gt;
&lt;#if "${isBig}"&gt;Wow!&lt;/#if&gt;
</pre>
应该写成：
<pre>&lt;#if isBig&gt;Wow!&lt;/#if&gt;
</pre>
<ul>
    <li>子串 </li>
</ul>
<p>例子（假设user的值为&#8220;Big Joe&#8221;）： </p>
<pre>${user[0]}${user[4]}
${user[1..4]}
</pre>
结果是（注意第一个字符的索引是0）：
<pre>BJ
ig J
</pre>
<strong>序列操作 </strong>
<ul>
    <li>连接操作：和字符串一样，使用+，下面是一个例子： </li>
</ul>
<pre>&lt;#list ["Joe", "Fred"] + ["Julia", "Kate"] as user&gt;
- ${user}
&lt;/#list&gt;
</pre>
输出结果是：
<pre>- Joe
- Fred
- Julia
- Kate
</pre>
<strong>散列操作 </strong>
<ul>
    <li>连接操作：和字符串一样，使用+，如果具有相同的key，右边的值替代左边的值，例如： </li>
</ul>
<pre>&lt;#assign ages = {"Joe":23, "Fred":25} + {"Joe":30, "Julia":18}&gt;
- Joe is ${ages.Joe}
- Fred is ${ages.Fred}
- Julia is ${ages.Julia}
</pre>
输出结果是：
<pre>- Joe is 30
- Fred is 25
- Julia is 18
</pre>
<strong>算术运算 </strong>
<ul>
    <li>＋、－、&#215;、／、％，下面是一个例子： </li>
</ul>
<pre>${x * x - 100}
${x / 2}
${12 % 10}
</pre>
输出结果是（假设x为5）：
<pre>-75
2.5
2
</pre>
操作符两边必须是数字，因此下面的代码是错误的：
<pre>${3 * "5"} &lt;#-- WRONG! --&gt;
</pre>
使用+操作符时，如果一边是数字，一边是字符串，就会自动将数字转换为字符串，例如：
<pre>${3 + "5"}
</pre>
输出结果是：
<pre>35
</pre>
使用内建的int（后面讲述）获得整数部分，例如：
<pre>${(x/2)?int}
${1.1?int}
${1.999?int}
${-1.1?int}
${-1.999?int}
</pre>
输出结果是（假设x为5）：
<pre>2
1
1
-1
-1
</pre>
<ul>
    <li>比较操作符 </li>
</ul>
<p>使用=（或==，完全相等）测试两个值是否相等，使用!= 测试两个值是否不相等 </p>
<p>=和!=两边必须是相同类型的值，否则会产生错误，例如&lt;#if 1 = "1"&gt;会引起错误 </p>
<p>Freemarker是精确比较，所以对"x"、"x "和"X"是不相等的 </p>
<p>对数字和日期可以使用&lt;、&lt;=、&gt;和&gt;=，但不能用于字符串 </p>
<p>由于Freemarker会将&gt;解释成FTL标记的结束字符，所以对于&gt;和&gt;=可以使用括号来避免这种情况，例如&lt;#if (x &gt; y)&gt; </p>
<p>另一种替代的方法是，使用lt、lte、gt和gte来替代&lt;、&lt;=、&gt;和&gt;= </p>
<ul>
    <li>逻辑操作符 </li>
</ul>
<p>&amp;&amp;（and）、||（or）、!（not），只能用于布尔值，否则会产生错误 </p>
<p>例子： </p>
<pre>&lt;#if x &lt; 12 &amp;&amp; color = "green"&gt;
We have less than 12 things, and they are green.
&lt;/#if&gt;
&lt;#if !hot&gt; &lt;#-- here hot must be a boolean --&gt;
It's not hot.
&lt;/#if&gt;
</pre>
<ul>
    <li>内建函数 </li>
</ul>
<p>内建函数的用法类似访问散列的子变量，只是使用&#8220;?&#8221;替代&#8220;.&#8221;，下面列出常用的一些函数 </p>
<ul>
    <li>
    <ul>
        <li>字符串使用的： </li>
    </ul>
    </li>
</ul>
<p>html：对字符串进行HTML编码 </p>
<p>cap_first：使字符串第一个字母大写 </p>
<p>lower_case：将字符串转换成小写 </p>
<p>upper_case：将字符串转换成大写 </p>
<p>trim：去掉字符串前后的空白字符 </p>
<ul>
    <li>
    <ul>
        <li>序列使用的： </li>
    </ul>
    </li>
</ul>
<p>size：获得序列中元素的数目 </p>
<ul>
    <li>
    <ul>
        <li>数字使用的： </li>
    </ul>
    </li>
</ul>
<p>int：取得数字的整数部分（如-1.9?int的结果是-1） </p>
<p>例子（假设test保存字符串"Tom &amp; Jerry"）： </p>
<pre>${test?html}
${test?upper_case?html}
</pre>
输出结果是：
<pre>Tom &amp;amp; Jerry
TOM &amp;amp; JERRY
</pre>
<ul>
    <li>操作符优先顺序 </li>
</ul>
<p>&nbsp;</p>
<table class="wikitable" border="1">
    <tbody>
        <tr>
            <th>操作符组 </th>
            <th>操作符 </th>
        </tr>
        <tr>
            <td>后缀 </td>
            <td>[subvarName] [subStringRange] . (methodParams) </td>
        </tr>
        <tr>
            <td>一元 </td>
            <td>+expr、-expr、! </td>
        </tr>
        <tr>
            <td>内建 </td>
            <td>? </td>
        </tr>
        <tr>
            <td>乘法 </td>
            <td>*、 / 、% </td>
        </tr>
        <tr>
            <td>加法 </td>
            <td>+、- </td>
        </tr>
        <tr>
            <td>关系 </td>
            <td>&lt;、&gt;、&lt;=、&gt;=（lt、lte、gt、gte） </td>
        </tr>
        <tr>
            <td>相等 </td>
            <td>==（=）、!= </td>
        </tr>
        <tr>
            <td>逻辑and </td>
            <td>&amp;&amp; </td>
        </tr>
        <tr>
            <td>逻辑or </td>
            <td>双竖线</td>
        </tr>
        <tr>
            <td>数字范围 </td>
            <td>.. </td>
        </tr>
    </tbody>
</table>
<p>&nbsp;</p>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-4Interpolation">（4）Interpolation </h4>
<p>Interpolation有两种类型： </p>
<ol>
    <li>通用Interpolation：${expr} </li>
</ol>
<ol>
    <li>数字Interpolation：#{expr}或#{expr; format} </li>
</ol>
<p>注意：Interpolation只能用于文本部分 </p>
<ul>
    <li>通用Interpolation </li>
</ul>
<p>插入字符串值：直接输出表达式结果 </p>
<p>插入数字值：根据缺省格式（由#setting指令设置）将表达式结果转换成文本输出；可以使用内建函数string格式化单个Interpolation，下面是一个例子： </p>
<pre>&lt;#setting number_format="currency"/&gt;
&lt;#assign answer=42/&gt;
${answer}
${answer?string}  &lt;#-- the same as ${answer} --&gt;
${answer?string.number}
${answer?string.currency}
${answer?string.percent}
</pre>
输出结果是：
<pre>$42.00
$42.00
42
$42.00
4,200%
</pre>
插入日期值：根据缺省格式（由#setting指令设置）将表达式结果转换成文本输出；可以使用内建函数string格式化单个Interpolation，下面是一个使用格式模式的例子：
<pre>${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}
${lastUpdated?string("EEE, MMM d, ''yy")}
${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}
</pre>
输出的结果类似下面的格式：
<pre>2003-04-08 21:24:44 Pacific Daylight Time
Tue, Apr 8, '03
Tuesday, April 08, 2003, 09:24:44 PM (PDT)
</pre>
插入布尔值：根据缺省格式（由#setting指令设置）将表达式结果转换成文本输出；可以使用内建函数string格式化单个Interpolation，下面是一个例子：
<pre>&lt;#assign foo=true/&gt;
${foo?string("yes", "no")}
</pre>
输出结果是：
<pre>yes
</pre>
<ul>
    <li>数字Interpolation的#{expr; format}形式可以用来格式化数字，format可以是： </li>
</ul>
<p>mX：小数部分最小X位 </p>
<p>MX：小数部分最大X位 </p>
<p>例子： </p>
<pre>&lt;#-- If the language is US English the output is: --&gt;
&lt;#assign x=2.582/&gt;
&lt;#assign y=4/&gt;
#{x; M2}   &lt;#-- 2.58 --&gt;
#{y; M2}   &lt;#-- 4    --&gt;
#{x; m1}   &lt;#-- 2.6 --&gt;
#{y; m1}   &lt;#-- 4.0 --&gt;
#{x; m1M2} &lt;#-- 2.58 --&gt;
#{y; m1M2} &lt;#-- 4.0  --&gt;
</pre>
<h3 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-4_E6_9D_82_E9_A1_B9">4、杂项 </h3>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-1_E7_94_A8_E6_88_B7_E5_AE_9A_E4_B9_89_E6_8C_87_E4_BB_A4">（1）用户定义指令 </h4>
<p>宏和变换器变量是两种不同类型的用户定义指令，它们之间的区别是宏是在模板中使用macro指令定义，而变换器是在模板外由程序定义，这里只介绍宏 </p>
<ul>
    <li>基本用法 </li>
</ul>
<p>宏是和某个变量关联的模板片断，以便在模板中通过用户定义指令使用该变量，下面是一个例子： </p>
<pre>&lt;#macro greet&gt;
&lt;font size="+2"&gt;Hello Joe!&lt;/font&gt;
&lt;/#macro&gt;
</pre>
作为用户定义指令使用宏变量时，使用@替代FTL标记中的#
<pre>&lt;@greet&gt;&lt;/@greet&gt;
</pre>
如果没有体内容，也可以使用：
<pre>&lt;@greet/&gt;
</pre>
<ul>
    <li>参数 </li>
</ul>
<p>在macro指令中可以在宏变量之后定义参数，如： </p>
<pre>&lt;#macro greet person&gt;
&lt;font size="+2"&gt;Hello ${person}!&lt;/font&gt;
&lt;/#macro&gt;
</pre>
可以这样使用这个宏变量：
<pre>&lt;@greet person="Fred"/&gt; and &lt;@greet person="Batman"/&gt;
</pre>
输出结果是：
<pre>  &lt;font size="+2"&gt;Hello Fred!&lt;/font&gt;
and   &lt;font size="+2"&gt;Hello Batman!&lt;/font&gt;
</pre>
<p>宏的参数是FTL表达式，所以下面的代码具有不同的意思： </p>
<pre>&lt;@greet person=Fred/&gt;
</pre>
这意味着将Fred变量的值传给person参数，该值不仅是字符串，还可以是其它类型，甚至是复杂的表达式
<p>可以有多参数，下面是一个例子： </p>
<pre>&lt;#macro greet person color&gt;
&lt;font size="+2" color="${color}"&gt;Hello ${person}!&lt;/font&gt;
&lt;/#macro&gt;
</pre>
可以这样使用该宏变量：
<pre>&lt;@greet person="Fred" color="black"/&gt;
</pre>
其中参数的次序是无关的，因此下面是等价的：
<pre>&lt;@greet color="black" person="Fred"/&gt;
</pre>
只能使用在macro指令中定义的参数，并且对所有参数赋值，所以下面的代码是错误的：
<pre>&lt;@greet person="Fred" color="black" background="green"/&gt;
&lt;@greet person="Fred"/&gt;
</pre>
可以在定义参数时指定缺省值，如：
<pre>&lt;#macro greet person color="black"&gt;
&lt;font size="+2" color="${color}"&gt;Hello ${person}!&lt;/font&gt;
&lt;/#macro&gt;
</pre>
这样&lt;@greet person="Fred"/&gt;就正确了
<p>宏的参数是局部变量，只能在宏定义中有效 </p>
<ul>
    <li>嵌套内容 </li>
</ul>
<p>用户定义指令可以有嵌套内容，使用&lt;#nested&gt;指令执行指令开始和结束标记之间的模板片断 </p>
<p>例子： </p>
<pre>&lt;#macro border&gt;
&lt;table border=4 cellspacing=0 cellpadding=4&gt;&lt;tr&gt;&lt;td&gt;
&lt;#nested&gt;
&lt;/tr&gt;&lt;/td&gt;&lt;/table&gt;
&lt;/#macro&gt;
</pre>
这样使用该宏变量：
<pre>&lt;@border&gt;The bordered text&lt;/@border&gt;
</pre>
输出结果：
<pre>  &lt;table border=4 cellspacing=0 cellpadding=4&gt;&lt;tr&gt;&lt;td&gt;
The bordered text
&lt;/tr&gt;&lt;/td&gt;&lt;/table&gt;
</pre>
<p>&lt;#nested&gt;指令可以被多次调用，例如： </p>
<pre>&lt;#macro do_thrice&gt;
&lt;#nested&gt;
&lt;#nested&gt;
&lt;#nested&gt;
&lt;/#macro&gt;
&lt;@do_thrice&gt;
Anything.
&lt;/@do_thrice&gt;
</pre>
输出结果：
<pre>  Anything.
Anything.
Anything.
</pre>
嵌套内容可以是有效的FTL，下面是一个有些复杂的例子： <tt><font face="新宋体">&lt;@border&gt; &lt;ul&gt; &lt;@do_thrice&gt; &lt;li&gt;&lt;@greet person="Joe"/&gt; &lt;/@do_thrice&gt; &lt;/ul&gt; &lt;/@border&gt; }}} 输出结果： </font>
<pre>  &lt;table border=4 cellspacing=0 cellpadding=4&gt;&lt;tr&gt;&lt;td&gt;
&lt;ul&gt;
&lt;li&gt;&lt;font size="+2"&gt;Hello Joe!&lt;/font&gt;
&lt;li&gt;&lt;font size="+2"&gt;Hello Joe!&lt;/font&gt;
&lt;li&gt;&lt;font size="+2"&gt;Hello Joe!&lt;/font&gt;
&lt;/ul&gt;
&lt;/tr&gt;&lt;/td&gt;&lt;/table&gt;
</pre>
宏定义中的局部变量对嵌套内容是不可见的，例如：
<pre>&lt;#macro repeat count&gt;
&lt;#local y = "test"&gt;
&lt;#list 1..count as x&gt;
${y} ${count}/${x}: &lt;#nested&gt;
&lt;/#list&gt;
&lt;/#macro&gt;
&lt;@repeat count=3&gt;${y?default("?")} ${x?default("?")} ${count?default("?")}&lt;/@repeat&gt;
</pre>
输出结果：
<pre>    test 3/1: ? ? ?
test 3/2: ? ? ?
test 3/3: ? ? ?
</pre>
<ul>
    <li>在宏定义中使用循环变量 </li>
</ul>
<p>用户定义指令可以有循环变量，通常用于重复嵌套内容，基本用法是：作为nested指令的参数传递循环变量的实际值，而在调用用户定义指令时，在&lt;@&#8230;&gt;开始标记的参数后面指定循环变量的名字 </p>
<p>例子： </p>
<pre>&lt;#macro repeat count&gt;
&lt;#list 1..count as x&gt;
&lt;#nested x, x/2, x==count&gt;
&lt;/#list&gt;
&lt;/#macro&gt;
&lt;@repeat count=4 ; c, halfc, last&gt;
${c}. ${halfc}&lt;#if last&gt; Last!&lt;/#if&gt;
&lt;/@repeat&gt;
</pre>
输出结果：
<pre>  1. 0.5
2. 1
3. 1.5
4. 2 Last!
</pre>
<p>指定的循环变量的数目和用户定义指令开始标记指定的不同不会有问题 </p>
<p>调用时少指定循环变量，则多指定的值不可见 </p>
<p>调用时多指定循环变量，多余的循环变量不会被创建 </p>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-2_E5_9C_A8_E6_A8_A1_E6_9D_BF_E4_B8_AD_E5_AE_9A_E4_B9_89_E5_8F_98_E9_87_8F">（2）在模板中定义变量 </h4>
<p>在模板中定义的变量有三种类型： </p>
<ul>
    <li>plain变量：可以在模板的任何地方访问，包括使用include指令插入的模板，使用assign指令创建和替换 </li>
</ul>
<ul>
    <li>局部变量：在宏定义体中有效，使用local指令创建和替换 </li>
</ul>
<ul>
    <li>循环变量：只能存在于指令的嵌套内容，由指令（如list）自动创建 </li>
</ul>
<p>宏的参数是局部变量，而不是循环变量；局部变量隐藏（而不是覆盖）同名的plain变量；循环变量隐藏同名的局部变量和plain变量，下面是一个例子： </p>
<pre>&lt;#assign x = "plain"&gt;
1. ${x}  &lt;#-- we see the plain var. here --&gt;
&lt;@test/&gt;
6. ${x}  &lt;#-- the value of plain var. was not changed --&gt;
&lt;#list ["loop"] as x&gt;
7. ${x}  &lt;#-- now the loop var. hides the plain var. --&gt;
&lt;#assign x = "plain2"&gt; &lt;#-- replace the plain var, hiding does not mater here --&gt;
8. ${x}  &lt;#-- it still hides the plain var. --&gt;
&lt;/#list&gt;
9. ${x}  &lt;#-- the new value of plain var. --&gt;
&lt;#macro test&gt;
2. ${x}  &lt;#-- we still see the plain var. here --&gt;
&lt;#local x = "local"&gt;
3. ${x}  &lt;#-- now the local var. hides it --&gt;
&lt;#list ["loop"] as x&gt;
4. ${x}  &lt;#-- now the loop var. hides the local var. --&gt;
&lt;/#list&gt;
5. ${x}  &lt;#-- now we see the local var. again --&gt;
&lt;/#macro&gt;
</pre>
输出结果：
<pre>1. plain
2. plain
3. local
4. loop
5. local
6. plain
7. loop
8. loop
9. plain2
</pre>
<p>内部循环变量隐藏同名的外部循环变量，如： </p>
<pre>&lt;#list ["loop 1"] as x&gt;
${x}
&lt;#list ["loop 2"] as x&gt;
${x}
&lt;#list ["loop 3"] as x&gt;
${x}
&lt;/#list&gt;
${x}
&lt;/#list&gt;
${x}
&lt;/#list&gt;
</pre>
输出结果：
<pre>  loop 1
loop 2
loop 3
loop 2
loop 1
</pre>
模板中的变量会隐藏（而不是覆盖）数据模型中同名变量，如果需要访问数据模型中的同名变量，使用特殊变量global，下面的例子假设数据模型中的user的值是Big Joe：
<pre>&lt;#assign user = "Joe Hider"&gt;
${user}          &lt;#-- prints: Joe Hider --&gt;
${.globals.user} &lt;#-- prints: Big Joe --&gt;
</pre>
<h4 id="section-FreeMarker_E8_AE_BE_E8_AE_A1_E6_8C_87_E5_8D_97-3_E5_90_8D_E5_AD_97_E7_A9_BA_E9_97_B4">（3）名字空间 </h4>
<p>通常情况，只使用一个名字空间，称为主名字空间 </p>
<p>为了创建可重用的宏、变换器或其它变量的集合（通常称库），必须使用多名字空间，其目的是防止同名冲突 </p>
<ul>
    <li>创建库 </li>
</ul>
<p>下面是一个创建库的例子（假设保存在lib/my_test.ftl中）： </p>
<pre>&lt;#macro copyright date&gt;
&lt;p&gt;Copyright (C) ${date} Julia Smith. All rights reserved.
&lt;br&gt;Email: ${mail}&lt;/p&gt;
&lt;/#macro&gt;
&lt;#assign mail = "jsmith@acme.com"&gt;
</pre>
使用import指令导入库到模板中，Freemarker会为导入的库创建新的名字空间，并可以通过import指令中指定的散列变量访问库中的变量：
<pre>&lt;#import "/lib/my_test.ftl" as my&gt;
&lt;#assign mail="fred@acme.com"&gt;
&lt;@my.copyright date="1999-2002"/&gt;
${my.mail}
${mail}
</pre>
输出结果：
<pre>  &lt;p&gt;Copyright (C) 1999-2002 Julia Smith. All rights reserved.
&lt;br&gt;Email: jsmith@acme.com&lt;/p&gt;
jsmith@acme.com
fred@acme.com
</pre>
可以看到例子中使用的两个同名变量并没有冲突，因为它们位于不同的名字空间
<p>可以使用assign指令在导入的名字空间中创建或替代变量，下面是一个例子： </p>
<pre>&lt;#import "/lib/my_test.ftl" as my&gt;
${my.mail}
&lt;#assign mail="jsmith@other.com" in my&gt;
${my.mail}
</pre>
输出结果：
<pre>jsmith@acme.com
jsmith@other.com
</pre>
数据模型中的变量任何地方都可见，也包括不同的名字空间，下面是修改的库：
<pre>&lt;#macro copyright date&gt;
&lt;p&gt;Copyright (C) ${date} ${user}. All rights reserved.&lt;/p&gt;
&lt;/#macro&gt;
&lt;#assign mail = "${user}@acme.com"&gt;
</pre>
假设数据模型中的user变量的值是Fred，则下面的代码：
<pre>&lt;#import "/lib/my_test.ftl" as my&gt;
&lt;@my.copyright date="1999-2002"/&gt;
${my.mail}
</pre>
输出结果：
<pre>  &lt;p&gt;Copyright (C) 1999-2002 Fred. All rights reserved.&lt;/p&gt;
Fred@acme.com   </pre>
</tt>
<img src ="http://www.blogjava.net/libin2722/aggbug/229263.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/libin2722/" target="_blank">礼物</a> 2008-09-16 21:56 <a href="http://www.blogjava.net/libin2722/articles/229263.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>