Velocity 入门之 HelloWorld
0
Velocity 是如何工作的呢? 虽然大多 Velocity 的应用都是基于 Servlet 的网页制作。但是为了说明 Velocity 的使用,我决定采用更通用的 Java application 来说明它的工作原理。
似乎所有语言教学的开头都是采用 HelloWorld 来作为第一个程序的示例。这里也不例外。
似乎所有语言教学的开头都是采用 HelloWorld 来作为第一个程序的示例。这里也不例外。
package com.makepolo.common.tools;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
public class CreateType {
public static void main(String[] args) throws Exception {
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("name", "yy");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
System.out));
Template template = Velocity.getTemplate("web/html/category/hello.vm");
template.merge(context, writer);
writer.flush();
writer.close();
}
} 模板文件:hello.vm中的内容为:Hello, $name 注意:Template template = Velocity.getTemplate("web/html/category/hello.vm");
这句是模板 hello.vm 的存放位置;web 是工程目录;