Velocity学习笔记(1)

记下我做的第一个小例子:

velocity的文档写得的确不是很好,废话一大堆,
看了大半天了连个小例子都做不出来,还抛异常...
网上一搜,居然有人因为这个原因转投freemarker门下了,velocity真是失败

步骤:
1.了解Dependencies:
Jakarta Commons Collections - required.
Jakarta Commons Lang - required.
Excalibur (ex-Avalon) Logkit - optional
Jakarta ORO - optional.

我们只需要前两个就可以了.
再加上velocity-1.6.3.jar

2.开发第一个例子:
basic/Basic.java

public static void singletonUsage(){
try {
Properties props=new Properties();
//set some properties to configure the velocity.


Velocity.init(props);

VelocityContext context = new VelocityContext();

context.put("name", new String("Velocity"));
context.put("project", new String("Jakarta"));

Template template = null;

template = Velocity.getTemplate("./src/basic/BasicUsage.vm");
//默认模板的路径是当前目录,在eclipse运行时就是工程的根目录了
//为了方便查看,把模板放在跟类同一个文件夹
//开头的说明里也不提下哪个参数可以改变这个配置.唉

StringWriter sw = new StringWriter();
template.merge(context, sw);

/* lets make our own string to render */
String s = "We are using $project $name to render this.";
Velocity.evaluate( context, sw, "logTagName", s );
//将template的输出加上s的输出,写到sw那里,然后打印出来.
System.out.println( sw );

} catch (Exception e) {
e.printStackTrace();
}
}

运行后报错!
org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.ServletLogChute with the current runtime configuration.
没事,作为coder,我们是一定要让自己爱上异常d,慢慢查吧
先google下,发现有几个人跟我同命相连,但是他们都没有解决..
靠自己吧,看了下velocity的例子,发现使用到了lib下的所有jar包
那我就先试试多加几个jar包进去吧,
嘿嘿,搞定,不抛异常了,输出结果:
Hi! This Velocity from the Jakarta project.
We are using Jakarta Velocity to render this.
实际上,多加commons-logging.jar一个jar就可以了,其他的不用加.


//模板位置对应的参数已经找到:file.resource.loader.path
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值