建立hello.vm内容如下:
Hello $name! Welcome to $site world!
main方法如下:
import java.io.File;
import java.io.StringWriter;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class HelloWorld{
public static void main( String[] args )throws Exception{
/* first, get and initialize an engine */
VelocityEngine ve = new VelocityEngine();
ve.init();
/* next, get the Template */
File file = new File("hello.vm");
System.out.println(file.exists());
Template t = ve.getTemplate( "hello.vm" );
/* create a context and add data */
VelocityContext context = new VelocityContext();
context.put("name", "Billy");
context.put("site", "http://www.sina.com");
/* now render the template into a StringWriter */
StringWriter writer = new StringWriter();
t.merge( context, writer );
/* show the World */
System.out.println( writer.toString() );
}
}
运行结果如下:
Hello Billy! Welcome to http://www.sina.com world!
Hello $name! Welcome to $site world!
main方法如下:
import java.io.File;
import java.io.StringWriter;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class HelloWorld{
public static void main( String[] args )throws Exception{
/* first, get and initialize an engine */
VelocityEngine ve = new VelocityEngine();
ve.init();
/* next, get the Template */
File file = new File("hello.vm");
System.out.println(file.exists());
Template t = ve.getTemplate( "hello.vm" );
/* create a context and add data */
VelocityContext context = new VelocityContext();
context.put("name", "Billy");
context.put("site", "http://www.sina.com");
/* now render the template into a StringWriter */
StringWriter writer = new StringWriter();
t.merge( context, writer );
/* show the World */
System.out.println( writer.toString() );
}
}
运行结果如下:
Hello Billy! Welcome to http://www.sina.com world!