您的位置:首页 > 其它

velocity初学指南2

2007-08-22 09:21 489 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: