当前位置:Gxlcms > PHP教程 > Velocity第一个应用例子

Velocity第一个应用例子

时间:2021-07-01 10:21:17 帮助过:37人阅读

Velocity第一个应用例子

执行的步骤

  1. /**
  2. * 1.Initialize Velocity. This applies to bothusage patterns for Velocity,
  3. * the Singleton as well as the 'separateruntime instance' (see more on
  4. * this below), and you only do this once.
  5. * 2. Create a Context object (more on whatthat is later).
  6. * 3.Add your data objects to the Context.
  7. * 4. Choose a template.
  8. * 5. 'Merge' the template and your data toproduce the
  9. ouput.
  10. *
  11. * @throws
  12. Exception
  13. */
  14. Velocity.init("src/velocity.properties");使用的是相对路径,项目下的src下的
  15. //2. Create a Context object
  16. VelocityContext context = newVelocityContext();
  17. //3.Add your data objects to this context
  18. context.put("hello","HeloWorld");
  19. context.put("who", "旗哥");
  20. //4.Choose a template
  21. Template template =Velocity.getTemplate("mytemplate.vm");
  22. StringWriter sw = new StringWriter();
  23. //5.Merge the template and you data toproduce the output
  24. template.merge(context, sw);
  25. sw.flush();
  26. System.out.println(sw.toString());

以上就是Velocity第一个应用例子的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行