Velocity之Hello World(入门实例)

参考:http://blog.csdn.net/mengxuwq/article/details/1871161 (很是感谢这篇文章,让我初步入门)java


本身调试彻底能运行后,写在此,供新人参考,供本身温故apache


一、准备模板文件编程

//以下为hellosite.vm
app

Hello $name! Welcome to $site world!

二、准备Java文件  (补充说明:hellosite.vm、HelloWorld.java放在同一目录下)spa

//以下为HelloWorld.java.net

import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

public class HelloWorld{
	public static void main( String[] args )throws Exception{
	   /* first, get and initialize an engine */
	   VelocityEngine ve = new VelocityEngine();
	   ve.init();
	   
	   /* next, get the Template */
	   Template t = ve.getTemplate( "hellosite.vm" );
	   
	   /* create a context and add data */
	   VelocityContext context = new VelocityContext();
	   context.put("name", "Eiffel Qiu");
	   context.put("site", "http://www.j2medev.com");
	   
	   /* now render the template into a StringWriter */
	   StringWriter writer = new StringWriter();
	   t.merge( context, writer );
	   
	   /* show the World */
	   System.out.println( writer.toString() ); 
	}
}

三、配置 Velocity所须要的jar包(本文的调试基于 velocity-1.6.3)

3.1 下载jar包调试

   http://velocity.apache.org/download.cgi ——>Older releases ——>最终到达 http://archive.apache.org/dist/velocity/engine/1.6.3/,找到 velocity-1.6.3.zip 下载code


3.1 解压velocity-1.6.3.zip ,发现解压后的目录下存在velocity-1.6.3.jar、velocity-1.6.3-dep.jarblog

3.2 配置classpath环境变量,引入velocity-1.6.3.jar、velocity-1.6.3-dep.jarip


四、编译运行上述Java文件,结果以下:

D:\聚划算\技术部\编程练习\Velocity\testVelocity>java HelloWor
Hello Eiffel Qiu! Welcome to http://www.j2medev.com world!


后记:

       一、调试过程当中,刚开始在classpath中只设置了velocity-1.6.3.jar,调试后发现缺class,缺的class正好在velocity-1.6.3-dep.jar中,引入后问题解决

相关文章
相关标签/搜索