﻿<?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           彬-文章分类-Freemarker</title><link>http://www.blogjava.net/libin2722/category/36802.html</link><description>虚其心，可解天下之问；专其心，可治天下之学；静其心，可悟天下之理；恒其心，可成天下之业。</description><language>zh-cn</language><lastBuildDate>Wed, 11 Aug 2010 02:38:08 GMT</lastBuildDate><pubDate>Wed, 11 Aug 2010 02:38:08 GMT</pubDate><ttl>60</ttl><item><title> FreeMarker 自定义标签</title><link>http://www.blogjava.net/libin2722/articles/328209.html</link><dc:creator>礼物</dc:creator><author>礼物</author><pubDate>Sat, 07 Aug 2010 10:11:00 GMT</pubDate><guid>http://www.blogjava.net/libin2722/articles/328209.html</guid><wfw:comment>http://www.blogjava.net/libin2722/comments/328209.html</wfw:comment><comments>http://www.blogjava.net/libin2722/articles/328209.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/libin2722/comments/commentRss/328209.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/libin2722/services/trackbacks/328209.html</trackback:ping><description><![CDATA[
		<h1 class="title_txt">					FreeMarker 自定义标签
					<cite class="fav_csdnstylebykimi"><a href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(saveit=window.open('http://wz.csdn.net/storeit.aspx?t='+escape(d.title)+'&amp;u='+escape(d.location.href)+'&amp;c='+escape(t),'saveit','scrollbars=no,width=590,height=300,left=75,top=20,status=no,resizable=yes'));saveit.focus();" class="fav_csdnstylebykimi" title="收藏到我的网摘中，并分享给我的朋友">收藏</a></cite></h1>
		<div class="blogstory">
				<script type="text/javascript"><![CDATA[
						document.body.oncopy = function () {
							if (window.clipboardData) {
								setTimeout(function () {
									var text = clipboardData.getData("text");
									if (text && text.length > 300) {
										text = text + "\r\n\n本文来自CSDN博客，转载请标明出处：" + location.href;
										clipboardData.setData("text", text);
									}
								}, 100);
							}
						}
					]]&gt;</script>
				<script type="text/javascript"><![CDATA[					function StorePage() { d = document; t = d.selection ? (d.selection.type != 'None' ? d.selection.createRange().text : '') : (d.getSelection ? d.getSelection() : ''); void (keyit = window.open('http://www.365key.com/storeit.aspx?t=' + escape(d.title) + '&u=' + escape(d.location.href) + '&c=' + escape(t), 'keyit', 'scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes')); keyit.focus(); }]]&gt;</script>
				<div class="sect1">
						<div class="titlepage">
								<div>
										<h2>
												<a name="pgui_datamodel_directive">
												</a>Directives</h2>
								</div>
						</div>
						<p>Java programmers can implement user-defined directives in Java using the <tt style="color: rgb(160, 61, 16);">TemplateDirectiveModel</tt> interface. See in the API documentation.</p>
						<div style="margin-left: 0.5in; margin-right: 0.5in;">
								<h3>Note</h3>
								<p>
										<tt style="color: rgb(160, 61, 16);">TemplateDirectiveModel</tt> was introduced in FreeMarker 2.3.11, replacing the soon to be depreciated <tt style="color: rgb(160, 61, 16);">TemplateTransformModel</tt>.</p>
						</div>
						<div class="titlepage">
								<div>
										<h3>
												<a name="autoid_35">
												</a>Example 1</h3>
								</div>
						</div>
						<p>We will implement a directive which converts all output between its start-tag and end-tag to upper case. Like, this template:</p>
						<div>
								<table bgcolor="#d8d8d8" border="0" cellpadding="0" cellspacing="0">
										<tbody>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td>
																<table style="margin: 0px; width: 100%;" bgcolor="#d8d8d8" border="0" cellpadding="4" cellspacing="0">
																		<tbody>
																				<tr>
																						<td>
																								<pre style="margin: 0px;">foo<br /><strong>&lt;@upper&gt;</strong><br />  bar<br />  &lt;#-- All kind of FTL is allowed here --&gt;<br />  &lt;#list ["red", "green", "blue"] as color&gt;<br />    ${color}<br />  &lt;/#list&gt;<br />  baaz<br /><strong>&lt;/@upper&gt;</strong><br />wombat <span style="font-size: 1pt;"></span></pre>
																						</td>
																				</tr>
																		</tbody>
																</table>
														</td>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
										</tbody>
								</table>
						</div>
						<p>will output this:</p>
						<div>
								<table bgcolor="#ccffcc" border="0" cellpadding="0" cellspacing="0">
										<tbody>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td>
																<table style="margin: 0px; width: 100%;" bgcolor="#ccffcc" border="0" cellpadding="4" cellspacing="0">
																		<tbody>
																				<tr>
																						<td>
																								<pre style="margin: 0px;">foo<br />  BAR<br />    RED<br />    GREEN<br />    BLUE<br />  BAAZ<br />wombat <span style="font-size: 1pt;"></span></pre>
																						</td>
																				</tr>
																		</tbody>
																</table>
														</td>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
										</tbody>
								</table>
						</div>
						<p>This is the source code of the directive class:</p>
						<div>
								<table bgcolor="#f8f8f8" border="0" cellpadding="0" cellspacing="0">
										<tbody>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td>
																<table style="margin: 0px; width: 100%;" bgcolor="#f8f8f8" border="0" cellpadding="4" cellspacing="0">
																		<tbody>
																				<tr>
																						<td>
																								<pre style="margin: 0px;">package com.example;<br />import java.io.IOException;<br />import java.io.Writer;<br />import java.util.Map;<br /><br />import freemarker.core.Environment;<br />import freemarker.template.TemplateDirectiveBody;<br />import freemarker.template.TemplateDirectiveModel;<br />import freemarker.template.TemplateException;<br />import freemarker.template.TemplateModel;<br />import freemarker.template.TemplateModelException;<br /><br />/**<br /> *  FreeMarker user-defined directive that progressively transforms<br /> *  the output of its nested content to upper-case.<br /> *  <br /> *  <br /> *  &lt;p&gt;&lt;b&gt;Directive info&lt;/b&gt;&lt;/p&gt;<br /> * <br /> *  &lt;p&gt;Directive parameters: None<br /> *  &lt;p&gt;Loop variables: None<br /> *  &lt;p&gt;Directive nested content: Yes<br /> */<br />public class UpperDirective implements TemplateDirectiveModel {<br /><br />    public void execute(Environment env,<br />            Map params, TemplateModel[] loopVars,<br />            TemplateDirectiveBody body)<br />            throws TemplateException, IOException {<br />        // Check if no parameters were given:<br />        if (!params.isEmpty()) {<br />            throw new TemplateModelException(<br />                    "This directive doesn't allow parameters.");<br />        }<br />        if (loopVars.length != 0) {<br />                throw new TemplateModelException(<br />                    "This directive doesn't allow loop variables.");<br />        }<br /><br />        // If there is non-empty nested content:<br />        if (body != null) {<br />            // Executes the nested body. Same as &lt;#nested&gt; in FTL, except<br />            // that we use our own writer instead of the current output writer.<br />            body.render(new UpperCaseFilterWriter(env.getOut()));<br />        } else {<br />            throw new RuntimeException("missing body");<br />        }<br />    }<br /><br />    /**<br />     * A {@link Writer} that transforms the character stream to upper case<br />     * and forwards it to another {@link Writer}.<br />     */ <br />    private static class UpperCaseFilterWriter extends Writer {<br /><br />        private final Writer out;<br /><br />        UpperCaseFilterWriter (Writer out) {<br />            this.out = out;<br />        }<br /><br />        public void write(char[] cbuf, int off, int len)<br />                throws IOException {<br />            char[] transformedCbuf = new char[len];<br />            for (int i = 0; i &lt; len; i++) {<br />                transformedCbuf[i] = Character.toUpperCase(cbuf[i + off]);<br />            }<br />            out.write(transformedCbuf);<br />        }<br /><br />        public void flush() throws IOException {<br />            out.flush();<br />        }<br /><br />        public void close() throws IOException {<br />            out.close();<br />        }<br />    }<br /><br />} <span style="font-size: 1pt;"></span></pre>
																						</td>
																				</tr>
																		</tbody>
																</table>
														</td>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
										</tbody>
								</table>
						</div>
						<p>Now we still need to create an instance of this class, and make this 
directive available to the template with the name "upper" (or with 
whatever name we want) somehow. A possible solution is to put the 
directive in the data-model:</p>
						<div>
								<table bgcolor="#f8f8f8" border="0" cellpadding="0" cellspacing="0">
										<tbody>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td>
																<table style="margin: 0px; width: 100%;" bgcolor="#f8f8f8" border="0" cellpadding="4" cellspacing="0">
																		<tbody>
																				<tr>
																						<td>
																								<pre style="margin: 0px;">root.put("upper", new com.example.UpperDirective()); <span style="font-size: 1pt;"></span></pre>
																						</td>
																				</tr>
																		</tbody>
																</table>
														</td>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
										</tbody>
								</table>
						</div>
						<p>But typically it is better practice to put commonly used directives into the <tt style="color: rgb(160, 61, 16);">Configuration</tt> as <a href="http://writeblog.csdn.net/pgui_config_sharedvariables.html">shared variables</a>.</p>
						<p>It is also possible to put the directive into an FTL library (collection of macros and like in a template, that you <tt style="color: rgb(160, 61, 16);">include</tt> or <tt style="color: rgb(160, 61, 16);">import</tt> in other templates) using the <a href="http://writeblog.csdn.net/ref_builtins_expert.html#ref_builtin_new"><tt>new</tt> built-in</a>:</p>
						<div>
								<table bgcolor="#d8d8d8" border="0" cellpadding="0" cellspacing="0">
										<tbody>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td>
																<table style="margin: 0px; width: 100%;" bgcolor="#d8d8d8" border="0" cellpadding="4" cellspacing="0">
																		<tbody>
																				<tr>
																						<td>
																								<pre style="margin: 0px;">&lt;#-- Maybe you have directives that you have implemented in FTL --&gt;<br />&lt;#macro something&gt;<br />  ...<br />&lt;/#macro&gt;<br /><br />&lt;#-- Now you can't use &lt;#macro upper&gt;, but instead you can: --&gt;<br />&lt;#assign upper = "com.example.UpperDirective"?new()&gt; <span style="font-size: 1pt;"></span></pre>
																						</td>
																				</tr>
																		</tbody>
																</table>
														</td>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
										</tbody>
								</table>
						</div>
						<div class="titlepage">
								<div>
										<h3>
												<a name="autoid_36">
												</a>Example 2</h3>
								</div>
						</div>
						<p>
								<span id="result_box" class="short_text">
										<span style="font-size: large;">
												<span>我们将创建一个指令的执行指定的次数（同样列出指令）随意分隔与一个&lt;hr&gt; - S的repetations输出，其嵌套的内容，再学习。</span>
												<span>让我们把这种指令“重复”。</span>
												<span style="background-color: rgb(255, 255, 255);">例如模板：</span>
										</span>
								</span>
						</p>
						<div>
								<table bgcolor="#d8d8d8" border="0" cellpadding="0" cellspacing="0">
										<tbody>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td>
																<table style="margin: 0px; width: 100%;" bgcolor="#d8d8d8" border="0" cellpadding="4" cellspacing="0">
																		<tbody>
																				<tr>
																						<td>
																								<pre style="margin: 0px;">&lt;#assign x = 1&gt;<br /><br /><strong>&lt;@repeat count=4&gt;</strong><br />  Test ${x}<br />  &lt;#assign x = x + 1&gt;<br /><strong>&lt;/@repeat&gt;</strong><br /><br /><strong>&lt;@repeat count=3 hr=true&gt;</strong><br />  Test<br /><strong>&lt;/@repeat&gt;</strong><br /><br /><strong>&lt;@repeat count=3; cnt&gt;</strong><br />  ${cnt}. Test<br /><strong>&lt;/@repeat&gt;</strong> <span style="font-size: 1pt;"></span></pre>
																						</td>
																				</tr>
																		</tbody>
																</table>
														</td>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
										</tbody>
								</table>
						</div>
						<p>Output:</p>
						<div>
								<table bgcolor="#ccffcc" border="0" cellpadding="0" cellspacing="0">
										<tbody>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td>
																<table style="margin: 0px; width: 100%;" bgcolor="#ccffcc" border="0" cellpadding="4" cellspacing="0">
																		<tbody>
																				<tr>
																						<td>
																								<pre style="margin: 0px;">  Test 1<br />  Test 2<br />  Test 3<br />  Test 4<br /><br />  Test<br />&lt;hr&gt;  Test<br />&lt;hr&gt;  Test<br /><br />  1. Test<br />  2. Test<br />  3. Test<br /> <span style="font-size: 1pt;"></span></pre>
																						</td>
																				</tr>
																		</tbody>
																</table>
														</td>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
										</tbody>
								</table>
						</div>
						<p>The class:</p>
						<div>
								<table bgcolor="#f8f8f8" border="0" cellpadding="0" cellspacing="0">
										<tbody>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td>
																<table style="margin: 0px; width: 100%;" bgcolor="#f8f8f8" border="0" cellpadding="4" cellspacing="0">
																		<tbody>
																				<tr>
																						<td>
																								<pre style="margin: 0px;">package com.example;<br />import java.io.IOException;<br />import java.io.Writer;<br />import java.util.Iterator;<br />import java.util.Map;<br /><br />import freemarker.core.Environment;<br />import freemarker.template.SimpleNumber;<br />import freemarker.template.TemplateBooleanModel;<br />import freemarker.template.TemplateDirectiveBody;<br />import freemarker.template.TemplateDirectiveModel;<br />import freemarker.template.TemplateException;<br />import freemarker.template.TemplateModel;<br />import freemarker.template.TemplateModelException;<br />import freemarker.template.TemplateNumberModel;<br /><br />/**<br /> * FreeMarker user-defined directive for repeating a section of a template,<br /> * optionally with separating the output of the repetations with<br /> * &lt;tt&gt;&amp;lt;hr&gt;&lt;/tt&gt;-s.<br /> *<br /> * <br /> * &lt;p&gt;&lt;b&gt;Directive info&lt;/b&gt;&lt;/p&gt;<br /> * <br /> * &lt;p&gt;Parameters:<br /> * &lt;ul&gt;<br /> *   &lt;li&gt;&lt;code&gt;count&lt;/code&gt;: The number of repetations. Required!<br /> *       Must be a non-negative number. If it is not a whole number then it will<br /> *       be rounded &lt;em&gt;down&lt;/em&gt;.<br /> *   &lt;li&gt;&lt;code&gt;hr&lt;/code&gt;: Tells if a HTML "hr" element could be printed between<br /> *       repetations. Boolean. Optional, defaults to &lt;code&gt;false&lt;/code&gt;. <br /> * &lt;/ul&gt;<br /> *<br /> * &lt;p&gt;Loop variables: One, optional. It gives the number of the current<br /> *    repetation, starting from 1.<br /> * <br /> * &lt;p&gt;Nested content: Yes<br /> */<br />public class RepeatDirective implements TemplateDirectiveModel {<br /><br />    private static final String PARAM_NAME_COUNT = "count";<br />    private static final String PARAM_NAME_HR = "hr";<br /><br />    public void execute(Environment env,<br />            Map params, TemplateModel[] loopVars,<br />            TemplateDirectiveBody body)<br />            throws TemplateException, IOException {<br /><br />        // ---------------------------------------------------------------------<br />        // Processing the parameters:<br /><br />        int countParam = 0;<br />        boolean countParamSet = false;<br />        boolean hrParam = false;<br /><br />        Iterator paramIter = params.entrySet().iterator();<br />        while (paramIter.hasNext()) {<br />            Map.Entry ent = (Map.Entry) paramIter.next();<br /><br />            String paramName = (String) ent.getKey();<br />            TemplateModel paramValue = (TemplateModel) ent.getValue();<br /><br />            if (paramName.equals(PARAM_NAME_COUNT)) {<br />                if (!(paramValue instanceof TemplateNumberModel)) {<br />                    throw new TemplateModelException(<br />                            "The \"" + PARAM_NAME_HR + "\" parameter "<br />                            + "must be a number.");<br />                }<br />                countParam = ((TemplateNumberModel) paramValue)<br />                        .getAsNumber().intValue();<br />                countParamSet = true;<br />                if (countParam &lt; 0) {<br />                    throw new TemplateModelException(<br />                            "The \"" + PARAM_NAME_HR + "\" parameter "<br />                            + "can't be negative.");<br />                }<br />            } else if (paramName.equals(PARAM_NAME_HR)) {<br />                if (!(paramValue instanceof TemplateBooleanModel)) {<br />                    throw new TemplateModelException(<br />                            "The \"" + PARAM_NAME_HR + "\" parameter "<br />                            + "must be a boolean.");<br />                }<br />                hrParam = ((TemplateBooleanModel) paramValue)<br />                        .getAsBoolean();<br />            } else {<br />                throw new TemplateModelException(<br />                        "Unsupported parameter: " + paramName);<br />            }<br />        }<br />        if (!countParamSet) {<br />                throw new TemplateModelException(<br />                        "The required \"" + PARAM_NAME_COUNT + "\" paramter"<br />                        + "is missing.");<br />        }<br /><br />        if (loopVars.length &gt; 1) {<br />                throw new TemplateModelException(<br />                        "At most one loop variable is allowed.");<br />        }<br /><br />        // Yeah, it was long and boring...<br /><br />        // ---------------------------------------------------------------------<br />        // Do the actual directive execution:<br /><br />        Writer out = env.getOut();<br />        if (body != null) {<br />            for (int i = 0; i &lt; countParam; i++) {<br />                // Prints a &lt;hr&gt; between all repetations if the "hr" parameter<br />                // was true:<br />                if (hrParam &amp;&amp; i != 0) {<br />                    out.write("&lt;hr&gt;");<br />                }<br /><br />                // Set the loop variable, if there is one:<br />                if (loopVars.length &gt; 0) {<br />                    loopVars[0] = new SimpleNumber(i + 1);<br />                }<br /><br />                // Executes the nested body (same as &lt;#nested&gt; in FTL). In this<br />                // case we don't provide a special writer as the parameter:<br />                body.render(env.getOut());<br />            }<br />        }<br />    }<br /><br />} <span style="font-size: 1pt;"></span></pre>
																						</td>
																				</tr>
																		</tbody>
																</table>
														</td>
														<td bgcolor="#000000" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
												<tr valign="top">
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
														<td bgcolor="#000000" height="1" width="1">
																<img src="http://writeblog.csdn.net/images/none.gif" alt="" border="0" height="1" hspace="0" width="1" />
														</td>
												</tr>
										</tbody>
								</table>
						</div>
						<div class="titlepage">
								<div>
										<h3>
												<a name="autoid_37">
												</a>Notices</h3>
								</div>
						</div>
						<p>
								<span id="result_box" class="medium_text">
										<span style="font-size: small;">
												<span>这是非常重要的一TemplateDirectiveModel对象通常不应该有状态。</span>
												<span style="background-color: rgb(255, 255, 255);">典型的错误是对在该对象的字段指令调用执行状态储存。</span>
										</span>
										<span style="font-size: small;">
												<span style="background-color: rgb(255, 255, 255);">对同一指令，或者指令嵌套调用看成是由多个线程同时访问共享变量使用的对象。<br /><br /></span>
												<span style="background-color: rgb(255, 255, 255);">可惜的是，TemplateDirectiveModel，就做不支持传递参数按位置（而不是名称）。</span>
												<span style="background-color: rgb(255, 255, 255);">这是固定的起价FreeMarker的2.4。</span>
										</span>
								</span>
						</p>
				</div>
		</div>
<img src ="http://www.blogjava.net/libin2722/aggbug/328209.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 18:11 <a href="http://www.blogjava.net/libin2722/articles/328209.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><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></channel></rss>