通用的 initializing Velocity 一般步骤如下:
设置你指定的配置参数值在文件org/apache/velocity/runtime/defaults/velocity.properties中,或放入java.util.Properties, 在第一次调用init( filename )或 init( Properties ) .
单独设置每个配置参数通过调用 setProperty(),最后调用init(). 这适合一些一有自己的CMS系统(configuration management system )程序。
运行时一但初始化, 你就可以开始工作了.. 只需要考虑组装什么样的数据对象到你的输出模板上, Velocity utility class 可以帮你更容易做到这些.这里有一些命令摘要描述如下 :
eva luate( Context context, Writer out, String logTag, String instring ) eva luate( Context context, Writer writer, String logTag, InputStream instream ) 这个命令用来修饰输入流,你可以将包含TVL的模板文件从内置的String对象、DB、或非文件系统的数据源输入,.
invokeVelocimacro( String vmName, String namespace, String params[], Context context, Writer writer ) 你可以直接访问Velocimacros. 也可以通过eva luate()命令来完成,这里你只需简单的传入VM文件名(模板文件), 创建一组VM参数放到Context,然后输出. 注意放入Context的参数必须是键-值对出现的.
mergeTemplate( String templateName, Context context, Writer writer ) 这个命令用来执行Velocity的模板合并和渲染功能. 它将应用context中的数据对象到指定文件名的模板中.将结果输出的指定的Writer. 当然,如果没有特别的需要,不建议这么做.
boolean templateExists( String name ) 检测当前配置的资源中,是否存在name的模板名.
这样,我们就更容易编写使用Velocity的java代码了. Here it is
import java.io.StringWriter;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
public class Example2
{
public static void main( String args[] )
{
/* first, we init the runtime engine. Defaults are fine. */
Velocity.init();
/* lets make a Context and put data into it */
VelocityContext context = new VelocityContext();
context.put("name", "Velocity");
context.put("project", "Jakarta");
/* lets render a template */
StringWriter w = new StringWriter();
Velocity.mergeTemplate("testtemplate.vm", context, w );
System.out.println(" template : " + w );
/* lets make our own string to render */
String s = "We are using $project $name to render this.";
w = new StringWriter();
Velocity.eva luate( context, w, "mystring", s );
System.out.println(" string : " + w );
}
}
运行如上程序,在运行程序的目录下将得到模板文件testtemplate.vm (默认配置中模板文件的输出位置是当前目录):
template : Hi! This Velocity from the Jakarta project.
string : We are using Jakarta Velocity to render this.
where the template we used, testtemplate.vm, is
Hi! This $name from the $project project.
在这里,我们不得不使用 mergeTemplate() and eva luate() in our program. 这主要是为了示例. 在一般应用中,这是很少使用到的,但我们提供了根据具体需要自由使用的途径.
这有点不和我们这篇文张本意的“基础功能指南”这一意图, 但我想这是必须知道的. 首先, 你得到一个放置了数据对象的context对象, 不用的是使用了命令 mergeTemplate(), mergeTemplate() 所做的工作是合并模板, 在运行时调用低层功能(lower-level). 接下来,通过eva luate()方法使用一个String动态生成模板.
这是同样简单的使用Velocity engine的方式,这些可选功能也许可以帮你做一些重复的工作,比如生成模板的模板:)
2.Exceptions
There are three exceptions that Velocity will throw during the parse / merge cycle在解析/合并(parse/merge)模板周期中,Velocity或能出现三个Velocity异常.另外,还可能会有 IO problems, etc. Velocity自定义的异常可以在package org.apache.velocity.exception:
ResourceNotFoundException 当velocity的资源管理器无法找到系统请求的资源时出现.
ParseErrorException 当parse模板文件中的 VTL 语法出错时出现.
MethodInvocationException Thrown when a method of object in the context thrown an exception during render time,当处理模板中,context中的对象的命令调用出错时.
当然,每一次出错时,相关的消息内容会保存到运行时的日志文件中,获取更多资料,请查看API文档。
3.其它细节
在以上的一些例程中,使用默认的properties配置你的程序非常方便. 但你可以根据自己的需要,用自己的配置文件通过在Velocity中调用init(String yourFileName)命令传入你的配置文件名