Velocity.evaluate

457 阅读1分钟
import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
 
public class Example2
{
    public static void main( String args[] )
    {
        /* 首先,初始化运行时引擎,使用默认的配置 */
 
        Velocity.init();
 
        /* 创建Context对象,然后把数据放进去 */
 
        VelocityContext context = new VelocityContext();
 
        context.put("name", "Velocity");
        context.put("project", "Jakarta");
 
        /* 渲染模板 */
 
        StringWriter w = new StringWriter();
 
        Velocity.mergeTemplate("testtemplate.vm", context, w );
        System.out.println(" template : " + w );
 
        /* 渲染字符串 */
 
        String s = "We are using $project $name to render this.";
        w = new StringWriter();
        Velocity.evaluate( context, w, "mystring", s );
        System.out.println(" string : " + w );
    }
}