參考: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() ); } }
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中,引入後問題解決