Velocity是一個java模板引擎,經過簡潔的語法能夠返回動態內容給瀏覽器使用,本系類是基於velocity官方文檔(就是照着翻譯,同時對不清楚的地方進行詳細講解),其實技術文檔我一直推崇看官方文檔,官方文檔更新及時同時講解也很詳細,可是主要是須要英語基礎哈哈,下面咱們就開始velocity的學習了。html
在學習以前咱們先下載jar包,因爲官方文檔是先進行語法學習(蛋蛋的憂桑~~~~),因此這裏我先進行簡單的velocity搭建, 使用的是servlet,貼代碼!java
1 Properties p = new Properties(); 2 p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 3 Velocity.init( p ); 4 VelocityContext context = new VelocityContext(); 5 context.put( "name", new String("Velocity") ); 6 Template template = null; 7 StringWriter sw = new StringWriter(); 8 PrintWriter out = response.getWriter(); 9 try 10 { 11 //template = Velocity.getTemplate("cn/dota/mytemplate.vm"); 12 Velocity.mergeTemplate("cn/dota/mytemplate.vm", context, out); 13 } 14 catch( ResourceNotFoundException rnfe ) 15 { 16 // couldn't find the template 17 } 18 catch( ParseErrorException pee ) 19 { 20 // syntax error: problem parsing the template 21 } 22 catch( MethodInvocationException mie ) 23 { 24 // something invoked in the template 25 // threw an exception 26 } 27 catch( Exception e ) 28 {}
這裏核心的就是 1-11行,這裏簡單的說下velocity加載模板只有文件路徑和類路徑(使用velocity-tool能夠增長web上下文路勁配置),第二行就是指定使用類路徑, context其實就是傳遞參數,好了簡單的環境配置好了,咱們就開始來學習le !!!!!!!!web
Referencesapache
VTL能夠提供容易最簡單最乾淨的方式講動態內容放入web應用,甚至一個沒有編程經驗的網頁開發者也能很快的使用VTL。編程
VTL使用references將動態內容嵌入到網頁裏面。變量是references的一種類型能夠關聯到java代碼中定義的變量,他也能夠在網頁中本身定義的參數瀏覽器
咱們開始運行第一個程序ide
1 <html> 2 <body> 3 #set( $foo = "Velocity" ) 4 Hello $foo World! 5 </body> 6 <html>
因爲這個代碼沒有使用java傳入的參數因此能夠直接運行,後臺模板配置好直接訪問吧 修改 $foo 等於的值那麼咱們就能夠看到不同的結果學習
單行註釋 #lua
多行註釋 #* 內容*#spa
固然也可使用#**內容*#
1 <html> 2 <body> 3 看到我1</br>##看不到我1 4 看到我2</br>#*看不到我1 *# 5 看到我3 6 #** 7 你看不到我3 8 *# 9 </body> 10 <html>
看下效果吧 這個比較簡單
在vtl中有三種類型的references:變量、屬性和方法
可使用字符a-z A-Z 數字0-9 中劃線 下劃線組成以下
$foo $mudSlinger $mud-slinger $mud_slinger $mudSlinger1
變量的值能夠是使用$set指令也能夠從java代碼獲取 如 #set( $foo = "bar" ) 那麼foo的值就被設置爲 bar
屬性的使用格式是$後面加變量 加 . 好比 $foo.bar
好比$foo.getBar() $foo.setBar("哈哈");
簡單點講velocity的後兩種類型其實就是能夠對應到java 的一個類, 此處有補充
咱們使用變量的時候能夠增長一個大括號{} 爲了防止咱們的變量和web中的字母混合在一塊兒 好比 ${foo}good 就會把foo翻譯 若是不加 velocity就會把整個翻譯 那就會有問題了(注意 在使用指令的時候不能加{}符號 不然是會報錯的)
咱們在使用的時候最好還能夠加上! 如 $!{foo} 或者 $!foo , 在使用的時候咱們知道若是沒有定義的東西velocity會直接顯示或者有些爲null 加上!用來強制把不存在的變量顯示爲空白
下面兩個代碼各位能夠看看效果
<html>
<body>
Hello $!foo World!
</body>
<html>
<html>
<body>
Hello $foo World!
</body>
<html>
velocity從1.6開始推出了嚴格模式, 從字面理解就是要求更嚴啦~好比上面出現的 未定義而使用的話就會直接泡出異常(配置屬性runtime.references.strict 設置爲true爲嚴格模式)
$foo ## Exception #set($bar = $foo) ## Exception #if($foo == $bar)#end ## Exception #foreach($item in $foo)#end ## Exception
如今你已經熟悉了三種使用方式那麼你就能夠快速的進行開發。同時velocity利用了java標準從而能夠更快的進行開發
$foo $foo.getBar() ## 也能夠是這樣 $foo.Bar $data.setUser("jon") ## 也能夠是這樣 #set( $data.User = "jon" ) $data.getRequest().getServerName() ## 也能夠是這樣 $data.Request.ServerName ## 也能夠是這樣 ${data.Request.ServerName}
其實至關於velocity本身後臺進行了一次轉化相似的javabean模式 實際上我以爲通常般看我的咯
指令前面都是加上#符號
#set( $primate = "monkey" )
#set( $customer.Behavior = $primate )
左邊能夠是變量或者對象屬性 而右邊只能是 變量,String,對象屬性,對象方法,數字,ArrayList,Map
下面是例子
#set( $monkey = $bill ) ## variable reference #set( $monkey.Friend = "monica" ) ## string literal #set( $monkey.Blame = $whitehouse.Leak ) ## property reference #set( $monkey.Plan = $spindoctor.weave($web) ) ## method reference #set( $monkey.Number = 123 ) ##number literal #set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList #set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map
#set( $foo = "bar" ) $foo #set( $blargh = '$foo' ) $blargh #** bar $foo **#
<html> <body> #if( $foo ) <strong>Velocity!</strong> #end </body> <html>
這裏執行的要求是1.foo是爲true的boolean 2.foo是一個不爲null,空的string或者集合 3.foo是一個不爲null的對象
看一下三種類型的簡單使用
#if( $foo < 10 ) <strong>Go North</strong> #elseif( $foo == 10 ) <strong>Go East</strong> #elseif( $bar == 6 ) <strong>Go South</strong> #else <strong>Go West</strong> #end
例子
<ul> #foreach( $product in $allProducts ) <li>$product</li> #end </ul>
allProducts 能夠是vector,hashtable,array
若是是hashtable那麼咱們還能夠獲取鍵值
<ul> #foreach( $key in $allProducts.keySet() ) <li>Key: $key -> Value: $allProducts.get($key)</li> #end </ul>
veloctiy還提供了簡單的方法來獲取循環的個數
<table> #foreach( $customer in $customerList ) <tr><td>$foreach.count</td><td>$customer.Name</td></tr> #end </table>
同時,velocity還有 $foreach.hasNext,$foreach.last,$foreach.first, 同時 你還能夠在循環的時候使用#break中斷
## list first 5 customers only #foreach( $customer in $customerList ) #if( $foreach.count > 5 ) #break #end $customer.Name #end
經過該指令能夠導入本身的文件(只能是靜態文件,就算導入了 .vm文件也是沒用的) 多個文件能夠用逗號隔開
#include("cn/dota/me.txt")
能夠導入動態模板,可是隻能傳遞一個參數用法和include同樣
能夠動態執行一個語句 相似於js中的eval
能夠定義一個相似宏的指令
#define( $block )Hello $who#end #set( $who = 'World!' ) $block