`
Billy_Ao
  • 浏览: 12577 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
社区版块
存档分类
最新评论

Velocity简单例子

    博客分类:
  • Java
阅读更多
建立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!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics