今天被上班没什么事做,就在研究公司使用了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