Velocity 入门之 HelloWorld
2015-06-11 22:46
176 查看
Velocity 是如何工作的呢? 虽然大多 Velocity 的应用都是基于 Servlet 的网页制作。但是为了说明 Velocity 的使用,我决定采用更通用的 Java application 来说明它的工作原理。
似乎所有语言教学的开头都是采用 HelloWorld 来作为第一个程序的示例。这里也不例外。
模板文件:hello.vm中的内容为:Hello, $name
注意:Template template = Velocity.getTemplate("web/html/category/hello.vm");
这句是模板 hello.vm 的存放位置;web 是工程目录;
参考:http://wenku.baidu.com/view/391fdd2531126edb6f1a102b.html
似乎所有语言教学的开头都是采用 HelloWorld 来作为第一个程序的示例。这里也不例外。
01 | package com.makepolo.common.tools; |
02 |
03 | import java.io.BufferedWriter; |
04 |
05 | import java.io.OutputStreamWriter; |
06 |
07 | import org.apache.velocity.Template; |
08 |
09 | import org.apache.velocity.VelocityContext; |
10 |
11 | import org.apache.velocity.app.Velocity; |
12 |
13 | public class CreateType { |
14 |
15 | public static void main(String[] args) throws Exception { |
16 |
17 | Velocity.init(); |
18 |
19 | VelocityContext context = new VelocityContext(); |
20 |
21 | context.put( "name" , "yy" ); |
22 |
23 | BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( |
24 | System.out)); |
25 |
26 | Template template = Velocity.getTemplate( "web/html/category/hello.vm" ); |
27 |
28 | template.merge(context, writer); |
29 |
30 | writer.flush(); |
31 |
32 | writer.close(); |
33 |
34 | } |
35 |
36 | } |
注意:Template template = Velocity.getTemplate("web/html/category/hello.vm");
这句是模板 hello.vm 的存放位置;web 是工程目录;
参考:http://wenku.baidu.com/view/391fdd2531126edb6f1a102b.html
相关文章推荐
- hiho一下 第四十九周 -题目1 : 欧拉路·一
- C Primer Plus 第五章习题
- 子线程调用Toast报Can't create handler inside ~~~ 错误
- 浅谈jsp、freemarker、velocity区别
- 利用OpenCV给图像添加标注
- NAND FLASH控制器
- IOS-多线程
- 认识自己——当断则断
- 联想y470p 使用Win8.1若干问题
- 教程:一分钟完成SiteMesh Template模板组合
- 2015061103 - apache httpclient jar包下载地址
- Java: 面向对象程序设计(上)
- acm-hdu5265解题报告
- C语言小问题
- 成功叛逃到hexo
- SiteMesh基本用法及示例
- VS2013MFC单文档工程学习笔记六 - 窗口的常用消息事件
- 测试发布Python随笔
- Struts2
- Completely change MACE timestamps?