今天被Velocity的问题折腾了老长时间

今天被上班没什么事做,就在研究公司使用了Velocity的一个框架,Velocity我也做过一些程序,但是没有自己设置过各种参数,因为看着文档,做了一个Application。

 

代码如下:

 

import java.io.StringWriter;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class VelocityTest {

	public static void main(String args[]) throws Exception {
		
		Properties   p=new   Properties();  
	    p.setProperty("file.resource.loader.path","E:\\velocityroot");   
	    
		/* first, get and initialize an engine */
        VelocityEngine ve = new VelocityEngine();
        ve.init();
        /* next, get the Template */
        Template t = ve.getTemplate( "test.vm" );
        /* create a context and& nbsp;add data */
        VelocityContext context = new VelocityContext();
        context.put("name", "Eiffel Qiu");
        context.put("site", "http://www.eiffelqiu.com");
        /* now render the template into a StringWriter */
        StringWriter writer = new StringWriter();
        t.merge( context, writer );
        /* show the World */
        System.out.println( writer.toString() ); 
	}

}

 

然后自信满满的开始运行,想不到报错了。说找不到test.vm这个资源。公司不能上网,等到下班,回家查找了一下资料,发现原来test.vm应该放在项目的根目录里面,而我放到SRC文件夹里面。

 

而且,其实我们还可以设置资源目录,如:

 

       Properties   p=new   Properties();  
	p.setProperty("file.resource.loader.path","E:\\velocityroot");   
	    
        /* first, get and initialize an engine */
        VelocityEngine ve = new VelocityEngine();
        ve.init(p);
 

 

这样我们就可以统一放VM文件的位置了。

 

另外,转了这篇文章 http://tntxia.iteye.com/blog/559428 有兴趣的朋友可以看一下,可以了解一下Velocity

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值