﻿<?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-CONAN ZONE-文章分类-模板</title><link>http://www.blogjava.net/conans/category/47040.html</link><description>你越挣扎我就越兴奋</description><language>zh-cn</language><lastBuildDate>Wed, 17 Nov 2010 02:10:50 GMT</lastBuildDate><pubDate>Wed, 17 Nov 2010 02:10:50 GMT</pubDate><ttl>60</ttl><item><title>在struts2下使用Velocity1.6, velocity tools 1.4的Layout功能</title><link>http://www.blogjava.net/conans/articles/338191.html</link><dc:creator>CONAN</dc:creator><author>CONAN</author><pubDate>Tue, 16 Nov 2010 08:34:00 GMT</pubDate><guid>http://www.blogjava.net/conans/articles/338191.html</guid><description><![CDATA[<p>struts2默认貌似只是支持VelocityLayoutServlet</p>
<p>那么如何使用VelocityLayoutServlet呢？</p>
<p>首先，要自己写点代码了，写个extends org.apache.struts2.dispatcher.VelocityResult 的类，取名曰：VelocityLayoutResult</p>
<p>实现如下：</p>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">package</span><span style="color: #000000;">&nbsp;struts2.velocity;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.io.OutputStreamWriter;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.io.StringWriter;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.io.Writer;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;javax.servlet.Servlet;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;javax.servlet.ServletContext;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;javax.servlet.http.HttpServletRequest;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;javax.servlet.http.HttpServletResponse;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;javax.servlet.jsp.JspFactory;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;javax.servlet.jsp.PageContext;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;org.apache.struts2.ServletActionContext;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;org.apache.struts2.dispatcher.VelocityResult;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;org.apache.struts2.views.JspSupportServlet;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;org.apache.struts2.views.velocity.VelocityManager;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;org.apache.velocity.Template;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;org.apache.velocity.context.Context;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;com.opensymphony.xwork2.ActionContext;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;com.opensymphony.xwork2.ActionInvocation;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;com.opensymphony.xwork2.util.ValueStack;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;com.opensymphony.xwork2.util.logging.Logger;<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;com.opensymphony.xwork2.util.logging.LoggerFactory;<br />
<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;VelocityLayoutResult&nbsp;</span><span style="color: #0000ff;">extends</span><span style="color: #000000;">&nbsp;VelocityResult&nbsp;{<br />
<br />
</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">final</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;serialVersionUID&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">6020934292083047099L</span><span style="color: #000000;">;<br />
<br />
</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">final</span><span style="color: #000000;">&nbsp;Logger&nbsp;LOG&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;LoggerFactory.getLogger(VelocityLayoutResult.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">);<br />
<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;String&nbsp;KEY_SCREEN_CONTENT&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">screen_content</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;String&nbsp;KEY_LAYOUT&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">layout</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">final</span><span style="color: #000000;">&nbsp;String&nbsp;PROPERTY_DEFAULT_LAYOUT&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">tools.view.servlet.layout.default.template</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">final</span><span style="color: #000000;">&nbsp;String&nbsp;PROPERTY_LAYOUT_DIR&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">tools.view.servlet.layout.directory</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">final</span><span style="color: #000000;">&nbsp;String&nbsp;PROPERTY_INPUT_ENCODING&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">input.encoding</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">final</span><span style="color: #000000;">&nbsp;String&nbsp;PROPERTY_OUTPUT_ENCODING&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">output.encoding</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">final</span><span style="color: #000000;">&nbsp;String&nbsp;PROPERTY_CONTENT_TYPE&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">default.contentType</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br />
<br />
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;VelocityManager&nbsp;velocityManager;<br />
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;String&nbsp;defaultLayout;<br />
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;String&nbsp;layoutDir;<br />
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;String&nbsp;inputEncoding;<br />
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;String&nbsp;outputEncoding;<br />
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;String&nbsp;contentType;<br />
<br />
@Override<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;setVelocityManager(VelocityManager&nbsp;mgr)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.velocityManager&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;mgr;<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">super</span><span style="color: #000000;">.setVelocityManager(mgr);<br />
}<br />
<br />
@Override<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;doExecute(String&nbsp;finalLocation,&nbsp;ActionInvocation&nbsp;invocation)&nbsp;</span><span style="color: #0000ff;">throws</span><span style="color: #000000;">&nbsp;Exception&nbsp;{<br />
&nbsp;&nbsp;&nbsp;ValueStack&nbsp;stack&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ActionContext.getContext().getValueStack();<br />
<br />
&nbsp;&nbsp;&nbsp;HttpServletRequest&nbsp;request&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ServletActionContext.getRequest();<br />
&nbsp;&nbsp;&nbsp;HttpServletResponse&nbsp;response&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ServletActionContext.getResponse();<br />
&nbsp;&nbsp;&nbsp;JspFactory&nbsp;jspFactory&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;ServletContext&nbsp;servletContext&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ServletActionContext.getServletContext();<br />
&nbsp;&nbsp;&nbsp;Servlet&nbsp;servlet&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;JspSupportServlet.jspSupportServlet;<br />
<br />
&nbsp;&nbsp;&nbsp;velocityManager.init(servletContext);<br />
<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">boolean</span><span style="color: #000000;">&nbsp;usedJspFactory&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;PageContext&nbsp;pageContext&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(PageContext)&nbsp;ActionContext.getContext().get(ServletActionContext.PAGE_CONTEXT);<br />
<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(pageContext&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;servlet&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;jspFactory&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;JspFactory.getDefaultFactory();<br />
&nbsp;&nbsp;&nbsp;&nbsp;pageContext&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;jspFactory.getPageContext(servlet,&nbsp;request,&nbsp;response,&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">,&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">8192</span><span style="color: #000000;">,&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;ActionContext.getContext().put(ServletActionContext.PAGE_CONTEXT,&nbsp;pageContext);<br />
&nbsp;&nbsp;&nbsp;&nbsp;usedJspFactory&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">try</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;encoding&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;getEncoding(finalLocation);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;contentType&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;getContentType(finalLocation);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(encoding&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contentType&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;contentType&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">;charset=</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;encoding;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;Template&nbsp;t&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;getTemplate(stack,&nbsp;velocityManager.getVelocityEngine(),&nbsp;invocation,&nbsp;finalLocation,&nbsp;encoding);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;Context&nbsp;context&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;createContext(velocityManager,&nbsp;stack,&nbsp;request,&nbsp;response,&nbsp;finalLocation);<br />
&nbsp;&nbsp;&nbsp;&nbsp;StringWriter&nbsp;stringWriter&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;StringWriter();<br />
&nbsp;&nbsp;&nbsp;&nbsp;t.merge(context,&nbsp;stringWriter);<br />
&nbsp;&nbsp;&nbsp;&nbsp;context.put(KEY_SCREEN_CONTENT,&nbsp;stringWriter.toString());<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;obj&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;context.get(KEY_LAYOUT);<br />
&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;layout&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(obj&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">&nbsp;:&nbsp;obj.toString();<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;defaultLayout&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(String)&nbsp;velocityManager.getVelocityEngine().getProperty(PROPERTY_DEFAULT_LAYOUT);<br />
&nbsp;&nbsp;&nbsp;&nbsp;layoutDir&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(String)&nbsp;velocityManager.getVelocityEngine().getProperty(PROPERTY_LAYOUT_DIR);<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">!</span><span style="color: #000000;">layoutDir.endsWith(</span><span style="color: #000000;">"</span><span style="color: #000000;">/</span><span style="color: #000000;">"</span><span style="color: #000000;">))&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;layoutDir&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">/</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">!</span><span style="color: #000000;">layoutDir.startsWith(</span><span style="color: #000000;">"</span><span style="color: #000000;">/</span><span style="color: #000000;">"</span><span style="color: #000000;">))&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;layoutDir&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">/</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;layoutDir;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;defaultLayout&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;layoutDir&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;defaultLayout;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(layout&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;layout&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;defaultLayout;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;layout&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;layoutDir&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;layout;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;Template&nbsp;layoutVm&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">try</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inputEncoding&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(String)&nbsp;velocityManager.getVelocityEngine().getProperty(PROPERTY_INPUT_ENCODING);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;layoutVm&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;getTemplate(stack,&nbsp;velocityManager.getVelocityEngine(),&nbsp;invocation,&nbsp;layout,&nbsp;inputEncoding);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">&nbsp;(Exception&nbsp;e)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOG.error(</span><span style="color: #000000;">"</span><span style="color: #000000;">VelocityLayoutResult:&nbsp;Can't&nbsp;load&nbsp;layout&nbsp;\</span><span style="color: #000000;">""</span><span style="color: #000000;">&nbsp;+&nbsp;layout&nbsp;+&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">\</span><span style="color: #000000;">"</span><span style="color: #000000;">:&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;e);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">!</span><span style="color: #000000;">layout.equals(defaultLayout))&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;layoutVm&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;getTemplate(stack,&nbsp;velocityManager.getVelocityEngine(),&nbsp;invocation,&nbsp;defaultLayout,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inputEncoding);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;outputEncoding&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(String)&nbsp;velocityManager.getVelocityEngine().getProperty(PROPERTY_OUTPUT_ENCODING);<br />
&nbsp;&nbsp;&nbsp;&nbsp;Writer&nbsp;writer&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;OutputStreamWriter(response.getOutputStream(),&nbsp;outputEncoding);<br />
&nbsp;&nbsp;&nbsp;&nbsp;response.setContentType(contentType);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;layoutVm.merge(context,&nbsp;writer);<br />
&nbsp;&nbsp;&nbsp;&nbsp;writer.flush();<br />
&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">&nbsp;(Exception&nbsp;e)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;LOG.error(</span><span style="color: #000000;">"</span><span style="color: #000000;">Unable&nbsp;to&nbsp;render&nbsp;Velocity&nbsp;Template,&nbsp;'</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;finalLocation&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">'</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;e);<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">throw</span><span style="color: #000000;">&nbsp;e;<br />
&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">finally</span><span style="color: #000000;">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(usedJspFactory)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jspFactory.releasePageContext(pageContext);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br />
}<br />
<br />
}</span></div>
<p><font style="background-color: #ffffff;" size="2" color="#000000">然后在struts.xml中就可以配置了，我的配置如下：</font></p>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">&lt;!</span><span style="color: #ff00ff;">DOCTYPE&nbsp;struts&nbsp;PUBLIC<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-//Apache&nbsp;Software&nbsp;Foundation//DTD&nbsp;Struts&nbsp;Configuration&nbsp;2.0//EN"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"http://struts.apache.org/dtds/struts-2.0.dtd"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">struts</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
<br />
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">constant&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="struts.velocity.toolboxlocation"</span><span style="color: #ff0000;">&nbsp;value</span><span style="color: #0000ff;">="WEB-INF/toolbox.xml"</span><span style="color: #ff0000;">&nbsp;</span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;"><br />
<br />
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">constant&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="struts.velocity.configfile"</span><span style="color: #ff0000;">&nbsp;value</span><span style="color: #0000ff;">="WEB-INF/velocity.properties"</span><span style="color: #ff0000;">&nbsp;</span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">include&nbsp;</span><span style="color: #ff0000;">file</span><span style="color: #0000ff;">="struts-default.xml"</span><span style="color: #ff0000;">&nbsp;</span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">package&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="default"</span><span style="color: #ff0000;">&nbsp;extends</span><span style="color: #0000ff;">="struts-default"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">result-types</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">result-type&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="velocity"</span><span style="color: #ff0000;">&nbsp;class</span><span style="color: #0000ff;">="struts2.velocity.VelocityLayoutResult"</span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">result-types</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">action&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="hello"</span><span style="color: #ff0000;">&nbsp;class</span><span style="color: #0000ff;">="test.Hello"</span><span style="color: #ff0000;">&nbsp;method</span><span style="color: #0000ff;">="execute"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">result&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="success"</span><span style="color: #ff0000;">&nbsp;type</span><span style="color: #0000ff;">="velocity"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">result.vm</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">result</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">action</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">package</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">struts</span><span style="color: #0000ff;">&gt;</span></div>
<p><br />
</p>
<p><font style="background-color: #ffffff;" size="2" color="#000000">接下来就可以开始velocity layout之旅了</font></p>
<p><font style="background-color: #ffffff;" size="2" color="#000000"><br />
</font></p>
<p><br />
</p>
<p><br />
</p>
<p><br />
</p>
<p><br />
</p>
<img src ="http://www.blogjava.net/conans/aggbug/338191.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/conans/" target="_blank">CONAN</a> 2010-11-16 16:34 <a href="http://www.blogjava.net/conans/articles/338191.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Velocity文档</title><link>http://www.blogjava.net/conans/articles/338094.html</link><dc:creator>CONAN</dc:creator><author>CONAN</author><pubDate>Mon, 15 Nov 2010 06:51:00 GMT</pubDate><guid>http://www.blogjava.net/conans/articles/338094.html</guid><description><![CDATA[Velocity是什么？<br />
Velocity是一个基于java的模板引擎（template engine）。它允许任何人仅仅简单的使用模板语言（template language）来引用由java代码定义的对象。<br />
当
Velocity应用于web开发时，界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点，也就是说，页面设计人员可以只关
注页面的显示效果，而由java程序开发人员关注业务逻辑编码。Velocity将java代码从web页面中分离出来，这样为web站点的长期维护提供
了便利，同时也为我们在JSP和PHP之外又提供了一种可选的方案。<br />
Velocity的能力远不止web站点开发这个领域，例如，它可以从模板
（template）产生SQL和PostScript、XML，它也可以被当作一个独立工具来产生源代码和报告，或者作为其他系统的集成组件使用。
Velocity也可以为Turbine web开发架构提供模板服务（template
service）。Velocity+Turbine提供一个模板服务的方式允许一个web应用以一个真正的MVC模型进行开发。<br />
<br />
Velocity能为我们作什么？<br />
The Mud Store Example <br />
假
设你是一家专门出售Mud的在线商店的页面设计人员，让我们暂且称它为&#8220;在线MUD商店&#8221;。你们的业务很旺，客户下了各种类型和数量的mud订单。他们都
是通过输入用户名和密码后才登陆到你的网站，登陆后就允许他们查看订单并购买更多的mud。现在，一种非常流行的mud正在打折销售。另外有一些客户规律
性的购买另外一种也在打折但是不是很流行的Bright Red
Mud，由于购买的人并不多所以它被安置在页面的边缘。所有用户的信息都是被跟踪并存放于数据库中的，所以某天有一个问题可能会冒出来：为什么不使用
velocity来使用户更好的浏览他们感兴趣的商品呢？<br />
Velocity使得web页面的客户化工作非常容易。作为一个web site的设计人员，你希望每个用户登陆时都拥有自己的页面。<br />
你会见了一些公司内的软件工程师，你发现他们每个人都同意客户应该拥有具有个性化的信息。那让我们把软件工程师应该作的事情发在一边，看一看你应该作些什么吧。<br />
你可能在页面内嵌套如下的VTL声明：<br />
&lt;html&gt;<br />
&lt;body&gt;<br />
Hello $customer.Name!<br />
&lt;table&gt;<br />
#foreach( $mud in $nudsOnSpecial ) <br />
#if ( $customer.hasPurchased( $mud ) ) <br />
&lt;tr&gt;&lt;td&gt;$flogger.getPromo( $mud )&lt;/td&gt;&lt;/tr&gt;<br />
#end<br />
#end<br />
&lt;/table&gt;<br />
<br />
Velocity Template Language(VTL):AN introduction<br />
VTL意味着提供最简单、最容易并且最整洁的方式合并页面动态内容。<br />
VTL
使用references来在web
site内嵌套动态内容，一个变量就是一种类型的reference。变量是某种类型的refreence，它可以指向java代码中的定义，或者从当前
页面内定义的VTL statement得到值。下面是一个VTL statement的例子，它可以被嵌套到HTML代码中：<br />
&nbsp;&nbsp;#set ( $a = &#8220;Velocity&#8221; )<br />
和
所有的VTL
statement一样，这个statement以＃字符开始并且包含一个directive：set。当一个在线用户请求你的页面时，Velocity
Templating Engine将查询整个页面以便发现所有＃字符，然后确定哪些是VTL statement，哪些不需要VTL作任何事情。<br />
＃字符后紧跟一个directive：set时，这个set directive使用一个表达式（使用括号封闭）――一个方程式分配一个值给变量。变量被列在左边，而它的值被列在右边，最后他们之间使用＝号分割。<br />
在上面的例子中，变量是$a，而它的值是Velocity。和其他的references一样以$字符开始，而值总是以双引号封闭。Velocity中仅有String可以被赋值给变量。<br />
记住以下的规则：<br />
使用$字符开始的references用于得到什么；使用#字符开始的directives用于作些什么。<br />
Hello Velocity World!<br />
一旦某个变量被分配了一个值，那么你就可以在HTML文件的任何地方引用它。在下面的例子中，一个值被分配给$foo变量，并在其后被引用。<br />
&lt;html&gt;<br />
&lt;body&gt;<br />
#set ( $foo = &#8220;Velocity&#8221; )<br />
Hello $foo World!<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
上面的实现结果是在页面上打印&#8220;Hello Velocity World！&#8221;<br />
为了使包含VTL directives的statement更具有可读性，我们鼓励你在新行开始每个VTL statement，尽管你不是必须这么作。Set directive将在后面详细描述。<br />
注释<br />
单行注释：<br />
&nbsp;&nbsp;## This is a single line comment.<br />
多行注释：<br />
&nbsp;&nbsp;#*<br />
&nbsp;&nbsp; Thus begins a multi-line comment. Online visitors won&#8217;t<br />
&nbsp;&nbsp; see this text because the Velocity Templating Engine will<br />
ignore it.<br />
&nbsp;&nbsp;*#<br />
文档格式：<br />
&nbsp;&nbsp;#**<br />
&nbsp;&nbsp; This is a VTL comment block and<br />
&nbsp;&nbsp; may be used to store such information<br />
as the document author and versioning<br />
&nbsp;&nbsp; information:<br />
&nbsp;&nbsp; @version 5<br />
&nbsp;&nbsp; @author<br />
&nbsp;&nbsp;*#<br />
<br />
References<br />
在
VTL中有三种类型的references：变量(variables)、属性(properties)、方法(methods)。作为一个使用VTL的
页面设计者，你和你的工程师必须就references的名称达成共识，以便你可以在你的template中使用它们。<br />
Everything coming to and from a reference被作为一个String对象处理。如果有一个对象$foo是一个Integer对象，那么Velocity将调用它的toString()方法将这个对象转型为String类型。<br />
&nbsp;&nbsp;变量 <br />
&nbsp;&nbsp;格式要求同java。<br />
&nbsp;&nbsp;属性 <br />
&nbsp;&nbsp;例子：<br />
&nbsp;&nbsp;&nbsp;&nbsp;$customer.Address<br />
&nbsp;&nbsp;&nbsp;&nbsp;$purchase.Total<br />
$customer.Address
有两种含义。它可以表示：查找hashtable对象customer中以Address为关键字的值；也可以表示调用customer对象的
getAddress()方法。当你的页面被请求时，Velocity将确定以上两种方式选用那种，然后返回适当的值。<br />
方法 <br />
一个方法就是被定义在java中的一段代码，并且它有完成某些有用工作的能力，例如一个执行计算和判断条件是否成立、满足等。方法是一个由$开始并跟随VTL标识符组成的References，一般还包括一个VTL方法体。例如：<br />
&nbsp;&nbsp;$customer.getAddress()<br />
&nbsp;&nbsp;$purchase.getTotal()<br />
&nbsp;&nbsp;$page.setTitle( &#8220;My Home Page&#8221; )<br />
&nbsp;&nbsp;$person.setAttributes( [&#8220;Strange&#8221;, &#8220;Weird&#8221;, &#8220;Excited&#8221;] )<br />
前两个例子$customer.getAddress()和$purchase.getTotal()看起来挺想上面的属性$customer.Address 和 $purchase.Total。如果你觉得他们之间有某种联系的话，那你是正确的。<br />
VTL属性可以作为VTL方法的缩写。$customer.Address属性和使用$customer.getAddress()方法具有相同的效果。如果可能的话使用属性的方式是比较合理的。属性和方法的不同点在于你能够给一个方法指定一个参数列表。<br />
正式reference标记<br />
reference的正是格式如下：<br />
&nbsp;&nbsp;${mudSlinger}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;变量<br />
&nbsp;&nbsp;${customer.Address}&nbsp;&nbsp;&nbsp;&nbsp;属性<br />
&nbsp;&nbsp;${purchase.getTotal()}&nbsp;&nbsp;&nbsp;&nbsp;方法<br />
非正是格式更见常用，但是有时还是使用正是格式比较适合。例如：你希望通过一个变量$vice来动态的组织一个字符串。<br />
&nbsp;&nbsp;Jack is a $vicemaniac.<br />
本来变量是$vice现在却变成了$vicemaniac，这样Veloctiy就不知道您到底要什么了。所以，应该使用正是格式书写<br />
&nbsp;&nbsp;Jack is a ${vice}maniac<br />
现在Velocity知道变量是$vice而不是$vicemaniac。<br />
Quiet reference notation<br />
例如：<br />
&nbsp;&nbsp;&lt;input type=&#8221;text&#8221; name=&#8221;email&#8221; value=&#8221;$email&#8221; /&gt;<br />
当页面的form被初始加载时，变量$email还没有值，这时你肯定是希望它能够显示一个blank text来代替输出&#8221;$email&#8221;这样的字段。那么使用quiet reference notation就比较合适。<br />
&nbsp;&nbsp;&lt;input type=&#8221;text&#8221; name=&#8221;email&#8221; value=&#8221;$!email&#8221;/&gt;<br />
这样文本框的初始值就不会是email而是空值了。<br />
正式和quiet格式的reference notation也可一同使用，像下面这样：<br />
&nbsp;&nbsp;&lt;input type=&#8221;text&#8221; name=&#8221;email&#8221; value=&#8221;$!{email}&#8221;/&gt;<br />
Getting literal <br />
Velocity使用特殊字符$和#来帮助它工作，所以如果要在template里使用这些特殊字符要格外小心。本节将讨论$字符。<br />
&nbsp;&nbsp;货币字符 <br />
在VTL中使用$2.5这样的货币标识是没有问题得的，VTL不会将它错认为是一个reference，因为VTL中的reference总是以一个大写或者小写的字母开始。<br />
Escaping valid VTL reference <br />
VTL中使用&#8220;"&#8221;作为逃逸符。<br />
例如：<br />
&nbsp;&nbsp;#set( $email = &#8220;foo&#8221; )<br />
&nbsp;&nbsp;$email<br />
&nbsp;&nbsp;"$email<br />
&nbsp;&nbsp;""$email<br />
&nbsp;&nbsp;"""$email<br />
将render为：<br />
&nbsp;&nbsp;foo<br />
&nbsp;&nbsp;$email<br />
&nbsp;&nbsp;"foo<br />
&nbsp;&nbsp;""$email<br />
如果email变量没有被定义则<br />
&nbsp;&nbsp;$email<br />
&nbsp;&nbsp;"$email<br />
&nbsp;&nbsp;""$email<br />
&nbsp;&nbsp;"""$email<br />
将被render为：<br />
&nbsp;&nbsp;$email<br />
&nbsp;&nbsp;"$email<br />
&nbsp;&nbsp;""$email<br />
&nbsp;&nbsp;"""$email<br />
注意：VTL中未被定义的变量将被认为是一个字符串，所以以下例子：<br />
&nbsp;&nbsp;#set( $foo = &#8220;gibbous&#8221; )<br />
&nbsp;&nbsp;$moon = $foo<br />
的输出结果是：<br />
$moon = gibbous<br />
Case substitution<br />
现在你已经对reference比较熟悉了，你可以将他们高效的应用于你的template了。Velocity利用了很多java规范以方便了设计人员的使用。例如：<br />
&nbsp;&nbsp;$foo<br />
&nbsp;&nbsp;$foo.getBar()<br />
&nbsp;&nbsp;## is the same as<br />
&nbsp;&nbsp;$foo.Bar<br />
<br />
&nbsp;&nbsp;$data.getUser(&#8220;jon&#8221;)<br />
&nbsp;&nbsp;## is the same as<br />
&nbsp;&nbsp;$data.User(&#8220;jon&#8221;)<br />
<br />
&nbsp;&nbsp;$data.getRequest().getServerName()<br />
&nbsp;&nbsp;# is the same as<br />
&nbsp;&nbsp;$data.Request.ServerName<br />
&nbsp;&nbsp;## is the same as<br />
&nbsp;&nbsp;${data.Request.ServerName}<br />
但是，注意VTL中不会将reference解释为对象的实例变量。例如：$foo.Name将被解释为Foo对象的getName（）方法，而不是Foo对象的Name实例变量。<br />
Directives<br />
Reference允许设计者使用动态的内容，而directive使得你可以应用java代码来控制你的显示逻辑，从而达到你所期望的显示效果。<br />
&nbsp;&nbsp;#set <br />
&nbsp;&nbsp;#set directive被用于设置一个reference的值。例如：<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $primate = &#8220;monkey&#8221; )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $customer.Behavior = $primate )<br />
赋值左侧的（LHS）必须是一个变量或者属性reference。右侧（RHS）可以是以下类型中一种：<br />
l&nbsp;&nbsp;变量reference<br />
l&nbsp;&nbsp;String literal<br />
l&nbsp;&nbsp;属性reference<br />
l&nbsp;&nbsp;方法reference<br />
l&nbsp;&nbsp;number literal<br />
l&nbsp;&nbsp;ArrayList<br />
下面是应用各种类型的RHS的例子：<br />
&nbsp;&nbsp;＃set ( $monkey = $bill ) ##变量reference<br />
&nbsp;&nbsp;＃set ( $monkey.Friend = &#8220;monica&#8221; ) ##String literal<br />
&nbsp;&nbsp;＃set ( $monkey.Blame = $whitehouse.Leak )##属性reference<br />
&nbsp;&nbsp;＃set ( $monkey.Plan = $spindoctor.weave($web) )##方法reference<br />
&nbsp;&nbsp;＃set ( $monkey.Number = 123 )##Number literal<br />
&nbsp;&nbsp;＃set ( $monkey.Say = [&#8220;Not&#8221;, $my, &#8220;fault&#8221;] )##ArrayList<br />
注意：最后一个例子的取值方法为：$monkey.Say.get(0)<br />
RHS也可以是一个简单的算术表达式：<br />
&nbsp;&nbsp;#set ( $value = $foo + 1 )<br />
&nbsp;&nbsp;#set ( $value = $bar -1 )<br />
#set ( $value = $foo * $bar )<br />
#set ( $value = $foo / $bar )<br />
如果你的RHS是一个null，VTL的处理将比较特殊：它将指向一个已经存在的reference，这对初学者来讲可能是比较费解的。例如：<br />
&nbsp;&nbsp;#set ( $resut = $query.criteria(&#8220;name&#8221;) )<br />
&nbsp;&nbsp;The result of the first query is $result<br />
<br />
&nbsp;&nbsp;#set ( $resut = $query.criteria(&#8220;address&#8221;) )<br />
&nbsp;&nbsp;The result of the second query is $result<br />
如果$query.criteria(&#8220;name&#8221;)返回一个&#8220;bill&#8221;，而$query.criteria(&#8220;address&#8221;)返回的是null，则显示的结果如下：<br />
&nbsp;&nbsp;The result of the first query is bill<br />
&nbsp;&nbsp;The result of the first query is bill<br />
看看下面的例子：<br />
&nbsp;&nbsp;#set( $criteria = ["name", "address"] )<br />
#foreach( $criterion in $criteria )<br />
#set( $result = $query.criteria($criterion) )<br />
&nbsp;&nbsp;#if( $result )<br />
&nbsp;&nbsp;Query was successful<br />
&nbsp;&nbsp; &nbsp;&nbsp;#end<br />
#end<br />
在
上面的例子中，程序将不能智能的根据$result的值决定查询是否成功。在$result被#set后（added to the
context），它不能被设置回null（removed from the
context）。打印的结果将显示两次查询结果都成功了，但是实际上有一个查询是失败的。<br />
为了解决以上问题我们可以通过预先定义的方式：<br />
&nbsp;&nbsp;#set( $criteria = [&#8220;name&#8221;, &#8220;address&#8221;] )<br />
&nbsp;&nbsp;#foreach( $criterion in $criteria )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set( $result = false )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set( $result = $query.criteria( $criterion ) )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#if( $result )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Query was successful<br />
&nbsp;&nbsp;&nbsp;&nbsp;#end<br />
&nbsp;&nbsp;#end<br />
&nbsp;&nbsp;String Literals <br />
&nbsp;&nbsp;当你使用#set directive，String literal封闭在一对双引号内。<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $directoryRoot = &#8220;www&#8221; )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $templateName = &#8220;index.vm&#8221; )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $template = &#8220;$directoryRoot/$tempateName&#8221; )<br />
&nbsp;&nbsp;&nbsp;&nbsp;$template<br />
&nbsp;&nbsp;上面这段代码的输出结果为：www/index.vm<br />
&nbsp;&nbsp;但是，当string literal被封装在单引号内时，它将不被解析：<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $foo = &#8220;bar&#8221; )<br />
&nbsp;&nbsp;&nbsp;&nbsp;$foo<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $blargh = &#8216;$foo&#8217; )<br />
&nbsp;&nbsp;结果：<br />
&nbsp;&nbsp;&nbsp;&nbsp;bar<br />
&nbsp;&nbsp;&nbsp;&nbsp;$foo<br />
&nbsp;&nbsp;上面这个特性可以通过修改velocity.properties文件的stringliterals.interpolate = false的值来改变上面的特性是否有效。<br />
条件语句<br />
&nbsp;&nbsp;if/elseif/else <br />
当一个web页面被生成时使用Velocity的#if directrive，如果条件成立的话可以在页面内嵌入文字。例如：<br />
&nbsp;&nbsp;#if ( $foo )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;strong&gt;Velocity!&lt;/strong&gt;<br />
&nbsp;&nbsp;#end<br />
上例中的条件语句将在以下两种条件下成立：<br />
l&nbsp;&nbsp;$foo是一个boolean型的变量，且它的值为true<br />
l&nbsp;&nbsp;$foo变量的值不为null<br />
这里需要注意一点：Velocity context仅仅能够包含对象，所以当我们说&#8220;boolean&#8221;时实际上代表的时一个Boolean对象。即便某个方法返回的是一个boolean值，Velocity也会利用内省机制将它转换为一个Boolean的相同值。<br />
如果条件成立，那么#if和#end之间的内容将被显示。<br />
#elseif和#else元素可以同#if一同使用。例如：<br />
&nbsp;&nbsp;#if( $foo &lt; 10 )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;strong&gt; Go North &lt;/strong&gt;<br />
&nbsp;&nbsp;#elseif( $foo == 10 )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;strong&gt; Go East &lt;/strong&gt;<br />
&nbsp;&nbsp;#elseif( $foo == 6 )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;strong&gt; Go South &lt;/strong&gt;<br />
&nbsp;&nbsp;#else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;strong&gt; Go West &lt;/strong&gt;<br />
&nbsp;&nbsp;#end<br />
注意这里的Velocity的数字是作为Integer来比较的――其他类型的对象将使得条件为false，但是与java不同它使用&#8220;＝＝&#8221;来比较两个值，而且velocity要求等号两边的值类型相同。<br />
关系、逻辑运算符 <br />
Velocity中使用等号操作符判断两个变量的关系。例如：<br />
#set ( $foo = &#8220;deoxyribonucleic acid&#8221; )<br />
#set ( $bar = &#8220;ribonucleic acid&#8221; )<br />
#if ( $foo == $foo )<br />
&nbsp;&nbsp;In this case it&#8217;s clear they aren&#8217;t equivalent.So&#8230;<br />
#else<br />
&nbsp;&nbsp;They are not equivalent and this will be the output.<br />
#end<br />
<br />
Velocity有AND、OR和NOT逻辑运算符。下面是一些例子：<br />
&nbsp;&nbsp;## logical AND<br />
&nbsp;&nbsp;#if( $foo &amp;&amp; $bar )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;strong&gt; This AND that &lt;/strong&gt;<br />
&nbsp;&nbsp;#end<br />
<br />
&nbsp;&nbsp;## logical OR<br />
&nbsp;&nbsp;#if ( $foo || $bar )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;strong&gt;This OR That &lt;/strong&gt;<br />
&nbsp;&nbsp;#end<br />
<br />
&nbsp;&nbsp;##logical NOT<br />
&nbsp;&nbsp;#if ( !$foo )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;strong&gt; NOT that &lt;/strong&gt;<br />
&nbsp;&nbsp;#end<br />
循环 <br />
&nbsp;&nbsp;Foreach循环 <br />
&nbsp;&nbsp;例子：<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;ul&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#foreach ( $product in $allProducts )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt; $product &lt;/li&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#end<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/ul&gt;<br />
&nbsp;&nbsp;每次循环$allProducts中的一个值都会赋给$product变量。<br />
$allProducts
可以是一个Vector、Hashtable或者Array。分配给$product的值是一个java对象，并且可以通过变量被引用。例如：如
果$product是一个java的Product类，并且这个产品的名字可以通过调用他的getName（）方法得到。<br />
现在我们假设$allProducts是一个Hashtable，如果你希望得到它的key应该像下面这样：<br />
&lt;ul&gt;<br />
#foreach ( $key in $allProducts.keySet() )<br />
&lt;li&gt;Key: $key -&gt; Value: $allProducts.get($key) &lt;/li&gt;<br />
#end<br />
&lt;/ul&gt;<br />
<br />
Velocity还特别提供了得到循环次数的方法，以便你可以像下面这样作：<br />
&lt;table&gt;<br />
#foreach ( $customer in $customerList )<br />
&lt;tr&gt;&lt;td&gt;$velocityCount&lt;/td&gt;&lt;td&gt;$customer.Name&lt;/td&gt;&lt;/tr&gt;<br />
#end<br />
&lt;/table&gt;<br />
$velocityCount
变量的名字是Velocity默认的名字，你也可以通过修改velocity.properties文件来改变它。默认情况下，计数从&#8220;1&#8221;开始，但是你
可以在velocity.properties设置它是从&#8220;1&#8221;还是从&#8220;0&#8221;开始。下面就是文件中的配置：<br />
&nbsp;&nbsp;# Default name of loop counter<br />
&nbsp;&nbsp;# variable reference<br />
&nbsp;&nbsp;directive.foreach.counter.name = velocityCount<br />
<br />
&nbsp;&nbsp;# Default starting value of the loop<br />
&nbsp;&nbsp;# counter variable reference<br />
&nbsp;&nbsp;directive.foreach.counter.initial.value = 1<br />
<br />
include<br />
#include script element允许模板设计者引入本地文件。被引入文件的内容将不会通过模板引擎被render。为了安全的原因，被引入的本地文件只能在TEMPLATE_ROOT目录下。<br />
&nbsp;&nbsp;#inclued ( &#8220;one.txt&#8221; )<br />
如果您需要引入多个文件，可以用逗号分隔就行：<br />
&nbsp;&nbsp;#include ( &#8220;one.gif&#8221;, &#8220;two.txt&#8221;, &#8220;three.htm&#8221; )<br />
在括号内可以是文件名，但是更多的时候是使用变量的：<br />
&nbsp;&nbsp;#inclue ( &#8220;greetings.txt&#8221;, $seasonalstock )<br />
<br />
parse<br />
#parse script element允许模板设计者一个包含VTL的本地文件。Velocity将解析其中的VTL并render模板。<br />
&nbsp;&nbsp;#parse( &#8220;me.vm&#8221; )<br />
就像#include，#parse接受一个变量而不是一个模板。任何由#parse指向的模板都必须包含在TEMPLATE_ROOT目录下。与#include不同的是，#parse只能指定单个对象。<br />
你可以通过修改velocity.properties文件的parse_direcive.maxdepth的值来控制一个template可以包含的最多#parse的个数――默认值是10。#parse是可以递归调用的，例如：如果dofoo.vm包含如下行：<br />
&nbsp;&nbsp;Count down.<br />
&nbsp;&nbsp;#set ( $count = 8 )<br />
&nbsp;&nbsp;#parse ( &#8220;parsefoo.vm&#8221; )<br />
&nbsp;&nbsp;All done with dofoo.vm!<br />
那么在parsefoo.vm模板中，你可以包含如下VTL：<br />
&nbsp;&nbsp;$count<br />
&nbsp;&nbsp;#set ( $count = $count &#8211; 1 )<br />
&nbsp;&nbsp;#if ( $count &gt; 0 )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#parse( &#8220;parsefoo.vm&#8221; )<br />
&nbsp;&nbsp;#else<br />
&nbsp;&nbsp;&nbsp;&nbsp;All done with parsefoo.vm!<br />
&nbsp;&nbsp;#end<br />
的显示结果为：<br />
&nbsp;&nbsp;Count down.<br />
&nbsp;&nbsp;8<br />
&nbsp;&nbsp;7<br />
&nbsp;&nbsp;6<br />
&nbsp;&nbsp;5<br />
&nbsp;&nbsp;4<br />
&nbsp;&nbsp;3<br />
&nbsp;&nbsp;2<br />
&nbsp;&nbsp;1<br />
&nbsp;&nbsp;0<br />
&nbsp;&nbsp;All done with parsefoo.vm!<br />
All done with dofoo.vm!<br />
<br />
Stop<br />
#stop script element允许模板设计者停止执行模板引擎并返回。把它应用于debug是很有帮助的。<br />
&nbsp;&nbsp;#stop<br />
<br />
Velocimacros<br />
#macro script element允许模板设计者定义一段可重用的VTL template。例如：<br />
&nbsp;&nbsp;#macro ( d )<br />
&nbsp;&nbsp;&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;<br />
&nbsp;&nbsp;#end<br />
在上面的例子中Velocimacro被定义为d，然后你就可以在任何VTL directive中以如下方式调用它：<br />
&nbsp;&nbsp;#d()<br />
当你的template被调用时，Velocity将用&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;替换为#d()。<br />
每个Velocimacro可以拥有任意数量的参数――甚至0个参数，虽然定义时可以随意设置参数数量，但是调用这个Velocimacro时必须指定正确的参数。下面是一个拥有两个参数的Velocimacro，一个参数是color另一个参数是array：<br />
&nbsp;&nbsp;#macro ( tablerows $color $somelist )<br />
&nbsp;&nbsp;#foreach ( $something in $somelist )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td bgcolor=$color&gt;$something&lt;/td&lt;/tr&gt;<br />
&nbsp;&nbsp;#end<br />
&nbsp;&nbsp;#end<br />
调用#tablerows Velocimacro：<br />
&nbsp;&nbsp;#set ( $greatlakes = [ &#8220;Superior&#8221;, &#8220;Michigan&#8221;, &#8220;Huron&#8221;, &#8220;Erie&#8221;, &#8220;Ontario&#8221; ] )<br />
&nbsp;&nbsp;#set ( $color = &#8220;blue&#8221; )<br />
&nbsp;&nbsp;&lt;table&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;#tablerows( $color $greatlakes )<br />
&nbsp;&nbsp;&lt;/table&gt;<br />
经过以上的调用将产生如下的显示结果：<br />
&nbsp;&nbsp;&lt;table&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td bgcolor=&#8221; blue&#8221;&gt; Superior &lt;/td&gt;&lt;/tr&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td bgcolor=&#8221; blue&#8221;&gt; Michigan &lt;/td&gt;&lt;/tr&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td bgcolor=&#8221; blue&#8221;&gt; Huron &lt;/td&gt;&lt;/tr&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td bgcolor=&#8221; blue&#8221;&gt; Erie &lt;/td&gt;&lt;/tr&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td bgcolor=&#8221; blue&#8221;&gt; Ontario &lt;/td&gt;&lt;/tr&gt;<br />
&nbsp;&nbsp;&lt;/table&gt;<br />
Velocimacros
可以在Velocity模板内实现行内定义（inline），也就意味着同一个web
site内的其他Velocity模板不可以获得Velocimacros的定义。定义一个可以被所有模板共享的Velocimacro显然是有很多好处
的：它减少了在一大堆模板中重复定义的数量、节省了工作时间、减少了出错的几率、保证了单点修改。<br />
上面定义的#tablerows(
$color $list
)Velocimacro被定义在一个Velocimacros模板库(在velocity.properties中定义)里，所以这个macro可以在
任何规范的模板中被调用。它可以被多次应用并且可以应用于不同的目的。例如下面的调用：<br />
&nbsp;&nbsp;#set ( $parts = [ &#8220;volva&#8221;, &#8220;stipe&#8221;, &#8220;annulus&#8221;, &#8220;gills&#8221;, &#8220;pileus&#8221; ] )<br />
&nbsp;&nbsp;#set ( $cellbgcol = &#8220;#CC00FF&#8221; )<br />
&nbsp;&nbsp;&lt;table&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;#tablerows( $cellbgcol $parts )<br />
&nbsp;&nbsp;&lt;/table&gt;<br />
上面VTL将产生如下的输出：<br />
&nbsp;&nbsp;&lt;table&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td bgcolor=&#8221;#CC00FF&#8221;&gt; volva &lt;/td&lt;/tr&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td bgcolor=&#8221;#CC00FF&#8221;&gt; stipe &lt;/td&lt;/tr&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td bgcolor=&#8221;#CC00FF&#8221;&gt; annulus &lt;/td&lt;/tr&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td bgcolor=&#8221;#CC00FF&#8221;&gt; gills &lt;/td&lt;/tr&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td bgcolor=&#8221;#CC00FF&#8221;&gt; pileus &lt;/td&lt;/tr&gt;<br />
&nbsp;&nbsp;&lt;/table&gt;<br />
&nbsp;&nbsp;Velocimacro arguments <br />
&nbsp;&nbsp;Velocimacro可以使用以下任何元素作为参数：<br />
l&nbsp;&nbsp;Reference：任何以$开头的reference<br />
l&nbsp;&nbsp;String literal：<br />
l&nbsp;&nbsp;Number literal：<br />
l&nbsp;&nbsp;IntegerRange：[1&#8230;.3]或者[$foo&#8230;.$bar]<br />
l&nbsp;&nbsp;对象数组：[&#8220;a&#8221;,&#8221;b&#8221;,&#8221;c&#8221;]<br />
l&nbsp;&nbsp;boolean值：true、false<br />
当
将一个reference作为参数传递给Velocimacro时，请注意reference作为参数时是以名字的形式传递的。这就意味着参数的值在每次
Velocimacro内执行时才会被产生。这个特性使得你可以将一个方法调用作为参数传递给Velocimacro，而每次Velocimacro执行
时都是通过这个方法调用产生不同的值来执行的。例如：<br />
&nbsp;&nbsp;#macro ( callme $a )<br />
&nbsp;&nbsp;&nbsp;&nbsp;$a $a $a<br />
&nbsp;&nbsp;#end<br />
&nbsp;&nbsp;#callme( $foo.bar() )<br />
执行的结果是：reference $foo的bar（）方法被执行了三次。<br />
如果你不需要这样的特性可以通过以下方法：<br />
&nbsp;&nbsp;#set ( $myval = $foo.bar() )<br />
&nbsp;&nbsp;#callme ( $myval )<br />
<br />
Velocimacro properties <br />
Velocity.properties文件中的某几行能够使Velocimacros的实现更加灵活。注意更多的内容可以看Developer Guide。<br />
Velocity.properties文件中的velocimacro.libraary：一个以逗号分隔的模板库列表。默认情况下，velocity查找唯一的一个库：VM_global_library.vm。你可以通过配置这个属性来指定自己的模板库。<br />
Velocity.properties
文件中的velocimacro.permissions.allow.inline属性：有两个可选的值true或者false，通过它可以确定
Velocimacros是否可以被定义在regular
template内。默认值是ture――允许设计者在他们自己的模板中定义Velocimacros。<br />
Velocity.properties文件中的<br />
velocimacro.permissions.allow.inline.replace.global
属性有两个可选值true和false，这个属性允许使用者确定inline的Velocimacro定义是否可以替代全局Velocimacro定义
（比如在velocimacro.library属性中指定的文件内定义的Velocimacro）。默认情况下，此值为false。这样就阻止本地
Velocimacro定义覆盖全局定义。<br />
Velocity.properties文件中的<br />
velocimacro.permissions.allow.inline.local.scale
属性也是有true和false两个可选值，默认是false。它的作用是用于确定你inline定义的Velocimacros是否仅仅在被定义的
template内可见。换句话说，如果这个属性设置为true，一个inline定义的Velocimacros只能在定义它的template内使
用。你可以使用此设置实现一个奇妙的VM敲门：a template can define a private implementation of
the second VM that will be called by the first VM when invoked by that
template. All other templates are unaffected。<br />
Velocity.properties文件中的
velocimacro.context.localscope属性有true和false两个可选值，默认值为false。当设置为true时，任何在
Velocimacro内通过#set()对context的修改被认为是针对此velocimacro的本地设置，而不会永久的影响内容。<br />
Velocity.properties
文件中的velocimacro.library.autoreload属性控制Velocimacro库的自动加载。默认是false。当设置为
ture时，对于一个Velocimacro的调用将自动检查原始库是否发生了变化，如果变化将重新加载它。这个属性使得你可以不用重新启动
servlet容器而达到重新加载的效果，就像你使用regular模板一样。这个属性可以使用的前提就是resource
loader缓存是off状态（file.resource.loader.cache = false）。注意这个属性实际上是针对开发而非产品的。<br />
Velocimacro Trivia <br />
Velocimacro必须被定义在他们被使用之前。也就是说，你的#macro()声明应该出现在使用Velocimacros之前。<br />
特
别要注意的是，如果你试图#parse()一个包含#macro()的模板。因为#parse()发生在运行期，但是解析器在parsetiem决定一个
看似VM元素的元素是否是一个VM元素，这样#parse()-ing一组VM声明将不按照预期的样子工作。为了得到预期的结果，只需要你简单的使用
velocimacro.library使得Velocity在启动时加载你的VMs。<br />
Escaping VTL directives<br />
VTL directives can be escaped with &#8220;"&#8221;号，使用方式跟VTL的reference使用逃逸符的格式差不多。<br />
&nbsp;&nbsp;## #include( &#8220;a.txt&#8221; ) renders as &lt;ontents of a.txt&gt;(注释行)<br />
&nbsp;&nbsp;#include( &#8220;a.txt&#8221; )<br />
<br />
&nbsp;&nbsp;## "#include( &#8220;a.txt&#8221; ) renders as "#include( &#8220;a.txt&#8221; )<br />
&nbsp;&nbsp;"#include( &#8220;a.txt&#8221; )<br />
<br />
&nbsp;&nbsp;## ""#include ( &#8220;a.txt&#8221; ) renders as "&lt;contents of a.txt&gt;<br />
&nbsp;&nbsp;""#include( &#8220;a.txt&#8221; )<br />
在对在一个directive内包含多个script元素的VTL directives使用逃逸符时要特别小心（比如在一个if-else-end statement内）。下面是VTL的if-statement的典型应用：<br />
&nbsp;&nbsp;#if ( $jazz )<br />
&nbsp;&nbsp;&nbsp;&nbsp;Vyacheslav Ganelin<br />
&nbsp;&nbsp;#end<br />
如果$jazz是ture，输出将是：<br />
&nbsp;&nbsp;Vyacheslav Ganelin<br />
如果$jazz是false，将没有输出。使用逃逸符将改变输出。考虑一下下面的情况：<br />
&nbsp;&nbsp;"#if ( $jazz )<br />
&nbsp;&nbsp;&nbsp;&nbsp;Vyacheslav Ganelin<br />
&nbsp;&nbsp;"#end<br />
现在无论$jazz是true还是false，输出结果都是：<br />
&nbsp;&nbsp;#if ( $jazz )<br />
&nbsp;&nbsp;&nbsp;&nbsp;Vyacheslav Ganelin<br />
&nbsp;&nbsp;#end<br />
事实上，由于你使用了逃逸符，$jazz根本就没有被解析为boolean型值。在逃逸符前使用逃逸符是合法的，例如：<br />
&nbsp;&nbsp;""#if ( $jazz )<br />
&nbsp;&nbsp;&nbsp;&nbsp;Vyacheslav Ganelin<br />
&nbsp;&nbsp;""#end<br />
以上程序的显示结果为：<br />
&nbsp;&nbsp;" Vyacheslav Ganelin<br />
&nbsp;&nbsp;"<br />
但是如果$jazz为false，那么将没有输出。（书上说会没有输出，但是我觉得应该还有有&#8220;"&#8221;字符被输出。）<br />
VTL：Formatting issues<br />
尽管在此用户手册中VTL通常都开始一个新行，如下所示：<br />
&nbsp;&nbsp;#set ( $imperial = [ &#8220;Munetaka&#8221;, &#8220;Koreyasu&#8221;, &#8220;Hisakira&#8221;, &#8220;Morikune&#8221; ] )<br />
&nbsp;&nbsp;#foreach ( $shogun in $imperial )<br />
&nbsp;&nbsp;&nbsp;&nbsp;$shogun<br />
&nbsp;&nbsp;#end<br />
但是像下面这种写法也是可以的：<br />
&nbsp;&nbsp;Send me #set($foo = [&#8220;$10 and&#8221;,&#8221;a cake&#8221;])#foreach($a in $foo)$a #end please.<br />
上面的代码可以被改写为：<br />
&nbsp;&nbsp;Send me<br />
&nbsp;&nbsp;#set ( $foo = [&#8220;$10 and &#8220;,&#8221;a cake&#8221;] )<br />
&nbsp;&nbsp;#foreach ( $a in $foo )<br />
&nbsp;&nbsp;&nbsp;&nbsp;$a<br />
&nbsp;&nbsp;#end<br />
&nbsp;&nbsp;please.<br />
或者<br />
&nbsp;&nbsp;Send me<br />
&nbsp;&nbsp;#set($foo = [&#8220;$10 and &#8220;,&#8221;a cake&#8221;])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#foreach （$a in $foo ）$a<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#end please.<br />
这两种的输出结构将一样。<br />
其他特性和杂项<br />
&nbsp;&nbsp;math &nbsp;&nbsp;在模板中可以使用Velocity内建的算术函数，如：加、减、乘、除<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $foo = $bar + 3 )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $foo = $bar - 4 )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $foo = $bar * 6 )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $foo = $bar / 2 )<br />
&nbsp;&nbsp;当执行除法时将返回一个Integer类型的结果。而余数你可以使用%来得到：<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $foo = $bar % 5 )<br />
在Velocity内使用数学计算公式时，只能使用像-n,-2,-1,0,1,2,n这样的整数，而不能使用其它类型数据。当一个非整型的对象被使用时它将被logged并且将以null作为输出结果。<br />
Range Operator<br />
Range operator可以被用于与#set和#foreach statement联合使用。对于处理一个整型数组它是很有用的，Range operator具有以下构造形式：<br />
&nbsp;&nbsp;[n..m]<br />
m和n都必须是整型，而m是否大于n则无关紧要。例子：<br />
&nbsp;&nbsp;First example:<br />
&nbsp;&nbsp;#foreach ( $foo in [1..5] )<br />
&nbsp;&nbsp;&nbsp;&nbsp;$foo<br />
&nbsp;&nbsp;#end<br />
<br />
&nbsp;&nbsp;Second example:<br />
&nbsp;&nbsp;#foreach ( $bar in [2..-2] )<br />
&nbsp;&nbsp;&nbsp;&nbsp;$bar<br />
&nbsp;&nbsp;#end<br />
<br />
&nbsp;&nbsp;Third example:<br />
&nbsp;&nbsp;#set ( $arr = [0..1] )<br />
&nbsp;&nbsp;#foreach ( $i in $arr )<br />
&nbsp;&nbsp;&nbsp;&nbsp;$i<br />
&nbsp;&nbsp;#end<br />
<br />
&nbsp;&nbsp;Fourth example:<br />
&nbsp;&nbsp;[1..3]<br />
上面四个例子的输出结果为：<br />
&nbsp;&nbsp;First example：<br />
&nbsp;&nbsp;1 2 3 4 5<br />
<br />
&nbsp;&nbsp;Second example：<br />
&nbsp;&nbsp;2 1 0 -1 -2<br />
<br />
&nbsp;&nbsp;Third example：<br />
&nbsp;&nbsp;0 1<br />
<br />
&nbsp;&nbsp;Fourth example：<br />
&nbsp;&nbsp;[1..3]<br />
注意：range operator只在#set和#foreach中有效。<br />
Advanced Issue：Escaping and！<br />
当一个reference被&#8220;！&#8221;分隔时，并且在它之前有逃逸符时，reference将以特殊的方式处理。注意这种方式与标准的逃逸方式时不同的。对照如下： <br />
#set ( $foo = &#8220;bar&#8221; )<br />
特殊形式&nbsp;&nbsp;标准格式<br />
Render前&nbsp;&nbsp;Render后&nbsp;&nbsp;Render前&nbsp;&nbsp;Render后<br />
$"!foo&nbsp;&nbsp;$!foo&nbsp;&nbsp;"$foo&nbsp;&nbsp;"$foo<br />
$"!{foo}&nbsp;&nbsp;$!{foo}&nbsp;&nbsp;"$!foo&nbsp;&nbsp;"$!foo<br />
$""!foo&nbsp;&nbsp;$"!foo&nbsp;&nbsp;"$!{foo}&nbsp;&nbsp;"$!{foo}<br />
$"""!foo&nbsp;&nbsp;$""!foo&nbsp;&nbsp;""$!{foo}&nbsp;&nbsp;"bar<br />
Velocimacro杂记<br />
&nbsp;&nbsp;Can I user a directive or another VM as an argument to a VM? <br />
&nbsp;&nbsp;例如：#center ( #bold( &#8220;hello&#8221; ) )<br />
&nbsp;&nbsp;不可以。一个directive的参数使用另外一个directive是不合法的。<br />
&nbsp;&nbsp;但是，还是有些事情你可以作的。最简单的方式就是使用双引号：<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $stuff = &#8220;#bold( &#8216;hello&#8217; )&#8221; )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#center ( $stuff ) <br />
&nbsp;&nbsp;上面的格式也可以缩写为一行：<br />
&nbsp;&nbsp;&nbsp;&nbsp;#center ( &#8220;#bold( &#8216;hello&#8217; ) )<br />
请注意在下面的例子中参数被evaluated在Velocimacro内部，而不是在calling level。例子：<br />
&nbsp;&nbsp;#macro ( inner $foo )<br />
&nbsp;&nbsp;&nbsp;&nbsp;inner : $foo<br />
&nbsp;&nbsp;#end<br />
<br />
&nbsp;&nbsp;#macro ( outer $foo )<br />
&nbsp;&nbsp;&nbsp;&nbsp;#set ( $bar = &#8220;outerlala&#8221; )<br />
&nbsp;&nbsp;&nbsp;&nbsp;outer : $foo<br />
&nbsp;&nbsp;#end<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;#set ( $bar = &#8216;calltimelala&#8217; )<br />
&nbsp;&nbsp;#outer( &#8220;#inner($bar)&#8221; )<br />
输出结果为：<br />
&nbsp;&nbsp;outer : inner : outerlala<br />
记住Veloctiy的特性：参数的传递是By Name的。例如：<br />
&nbsp;&nbsp;#macro ( foo $color )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr bgcolor = $color &gt;&lt;td&gt;Hi&lt;/td&gt;&lt;/tr&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr bgcolor = $color &gt;&lt;td&gt;There&lt;/td&gt;&lt;/tr&gt;<br />
&nbsp;&nbsp;#end<br />
<br />
&nbsp;&nbsp;#foo ( $bar.rowColor() )<br />
以上代码将导致rowColor()方法两次调用，而不是一次。为了避免这种现象的出现，我们可以按照下面的方式执行：<br />
&nbsp;&nbsp;#set ( $color = $bar.rowColor() )<br />
&nbsp;&nbsp;#foo ( $color )<br />
can I register velocimacros via #parse()? <br />
目前，Velocimacros必须在第一次被模板调用前被定义。这就意味着你的#macro()声明应该出现在使用Velocimacros之前。<br />
如
果你试图#parse()一个包含#macro()
directive的模板，这一点是需要牢记的。因为#parse()发生在运行期，但是解析器在parsetiem决定一个看似VM元素的元素是否是一
个VM元素，这样#parse()-ing一组VM声明将不按照预期的样子工作。为了得到预期的结果，只需要你简单的使用
velocimacro.library使得Velocity在启动时加载你的VMs。<br />
What is velocimacro autoreloading？ <br />
velocimacro.library.autoreload是专门为开发而非产品使用的一个属性。此属性的默认值是false。<br />
String concatenation<br />
开发人员最常问的问题是我如何作字符拼接？在java中是使用&#8220;＋&#8221;号来完成的。<br />
在VTL里要想实现同样的功能你只需要将需要联合的reference放到一起就行了。例如：<br />
#set ( $size = &#8220;Big&#8221; )<br />
#set ( $name = &#8220;Ben&#8221; )<br />
The clock is $size$name.<br />
输出结果将是：The clock is BigBen.。更有趣的情况是：<br />
&nbsp;&nbsp;#set ( $size = &#8220;Big&#8221; )<br />
&nbsp;&nbsp;#set ( $name = &#8220;Ben&#8221; )<br />
&nbsp;&nbsp;#set ( $clokc = &#8220;$size$name&#8221; )<br />
&nbsp;&nbsp;The clock is $clock.<br />
上例也会得到同样的结果。最后一个例子，当你希望混合固定字段到你的reference时，你需要使用标准格式：<br />
&nbsp;&nbsp;#set ( $size = &#8220;Big&#8221; )<br />
&nbsp;&nbsp;#set ( $name = &#8220;Ben&#8221; )<br />
&nbsp;&nbsp;#set ( $clock = &#8220;${size}Tall$name&#8221; )<br />
&nbsp;&nbsp;The clock is $clock.<br />
输出结果是：The clock is BigTallBen.。使用这种格式主要是为了使得$size不被解释为$sizeTall。
<img src ="http://www.blogjava.net/conans/aggbug/338094.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/conans/" target="_blank">CONAN</a> 2010-11-15 14:51 <a href="http://www.blogjava.net/conans/articles/338094.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>