velocity初学指南2
Hello from $name in the $project project.
××××××××××××××××
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import java.io.StringWriter;
/**
* Created by IntelliJ IDEA.
* User: LEO
* Date: 2005-11-22
* Time: 20:22:41
* To change this template use File | Settings | File Templates.
*/
public class MyTest {
public static void main(String[] args) {
try
{
Velocity.init();
}
catch(Exception e)
{
System.out.println("Problem initializing Velocity : " + e );
return;
}
VelocityContext context = new VelocityContext();
context.put("name", "Velocity");
context.put("project", "LEO");
StringWriter w = new StringWriter();
try
{
Velocity.mergeTemplate("example2.vm","gb2312", context, w );
}
catch (Exception e )
{
System.out.println("Problem merging template : " + e );
}
System.out.println(" template : " + w );
String s = "We are using $project $name to render this.";
w = new StringWriter();
try
{
Velocity.evaluate( context, w, "mystring", s );
}
catch( ParseErrorException pee )
{
System.out.println("ParseErrorException : " + pee );
}
catch( MethodInvocationException mee )
{
System.out.println("MethodInvocationException : " + mee );
}
catch( Exception e )
{
System.out.println("Exception : " + e );
}
System.out.println(" string : " + w );
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=535805