Package com.velocity.test;
Import Java.io.StringWriter;
Import java.util.Properties;
Import Org.apache.velocity.VelocityContext;
Import Org.apache.velocity.app.VelocityEngine;
/**
* Load template file from file
*122_ADD.VM in the web-inf/vm/folder * @author Welcome
*
*/
public class Loaderfromfile {
public static void Main (string[] args) throws exception{
Initialize parameters
Properties Properties=new properties ();
Set the velocity resource load mode to class
Properties.setproperty ("Resource.loader", "file");
To set the processing class when the velocity resource is loaded as file
Properties.setproperty (Velocity.file_resource_loader_path,
This.getclass (). GetResource ("/"). ToString (). ReplaceAll ("^file:/", "" "). ReplaceAll (" Classes "," VM "));
Instantiate a Velocityengine object
Velocityengine velocityengine=new Velocityengine (properties);
Instantiate a Velocitycontext
Velocitycontext context=new Velocitycontext ();
Put a key value into the Velocitycontext
Context.put ("username", "Zhang San");
Context.put ("Password", "123456789");
Context.put ("Age", "20");
Context.put ("Address", "Xi ' an, Shaanxi");
Context.put ("blog", "http://blogjava.net/sxyx2008");
Instantiate a StringWriter
StringWriter writer=new StringWriter ();
Template Template =velocityengine.gettemplate ("122_ADD.VM");
Template.merge (context, writer);
System.out.println (writer);
Velocityengine.mergetemplate ("Hello.vm", "GBK", context, writer);
System.out.println (Writer.tostring ());
}
}