Velocity学习笔记(2) - 搭建最简单的学习环境

昨天还不了解velocity,稍微抱怨了下,嘿
经过一番探索之后,发现其实velocity还是很简单,很好入门的哈.

搭建最简单的学习环境

1.jar包是少不了的
Commons-Collections.jar
Commons-Common.jar
Commons-Lang.jar
velocity-1.6.3.jar
velocity-tools-1.4.jar
2.随便建个类:

public static void main(String[] args) {
try {
Velocity.init();//
basicUsage();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void basicUsage() throws IOException {

String template;
Writer writer;
VelocityContext context;
writer= new StringWriter();
context=new VelocityContext();
//需要什么参数这里加.
context.put("toolName", new String("Velocity"));

//访问常量 ${runtime.COUNTER_NAME}.
context.put("runtime", new FieldMethodizer( "org.apache.velocity.runtime.Runtime" ));

//使用velocity的tools也很简单:
//不知道为什么velocity的文档非要我们通过servlet来使用它的tools.
//还搞了个toolbox.xml来迷惑我.
context.put("date", new org.apache.velocity.tools.generic.DateTool());

//其他class同样可以加到context,只要你有需要.

template="We are using $toolName now.";
//模板写这里,想怎样写就怎样写,
//学习而已嘛,不需要使用模板文件,多麻烦呀.
writer.write("Template : ");
writer.write(template);
writer.write("\nOutput : ");

Velocity.evaluate( context, writer, "logTagName", template );
System.out.println( writer );
writer.close();
}

3.剩下的就是学习vtl的使用了,
模仿上面的basicUsage()来测试,学习就是了.
4.基本掌握VTL之后,再看下velocity.properties怎样配置,然后就可以正式投产了.
5.struts2已经整合好veleocity,直接用就是了
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值