- 定義
Velocity是一個基於java的模板引擎,它允許任何人僅僅使用模板語言來引用由java代碼定義的對象. 它還可以從模板中生成SQL語句或其它腳本提供給web pages. 它也可以獨立使用---做爲一個工具類(utility class)用來生成源代碼、報表、郵件模板等.
- VTL語法
- 註釋
- 單行註釋 “##”
- 註釋
## 頁面對變量賦值
-
-
- 多行註釋 “#**#”
-
#*頁面對變量賦值*#
-
- 引用(Reference)
- 變量
- 引用(Reference)
格式:$ VTL標識符
VTL標識符:字母、數字、”_”、”-“組成
#set($company = "merit") $monn = $company
一般方式:$mud-Slinger_9
靜態(輸出原始字面):$!mud-Slinger_9
正規格式:${mud-Slinger_9}
-
-
- 屬性
-
格式:$VTL標識符.VTL標識符
$monn.Address $monn.getAddress()(getter()方法)
-
-
- 方法
-
格式:$VTL標識符(參數列表)
$monn.getAddress() $page.setTitile("My Home") $person.setAttributes(["page1","page2","page3"])
-
- 指令(Directives)
- #set 變量的定義
- 指令(Directives)
格式:#set(LHS = RHS)
LHS:變量引用、屬性引用、簡單算術表達式
RHS:引用、字符串、數字、ArralList、Map,如果RHS的結果爲null,是不會賦值給LHS。
String:
雙引號:原樣輸出
單引號:會被替換成相應的值
#set($monkey = $bill) ##變量引用 #set($monkey.Friend = "cat") ##字符串 #set($monkey.Blame = $cat.Blame) ##屬性應用 #set($monkey.Number = 123) ##數字 #set($monkry.say = ["Not",$my,"$ga"]) ##集合 #set($value = $foo+1) ##加法運算 #set($root = "www") #set($tem = "index.vm") #set($template = $root/$tem)##引用的拼接,輸出結果爲www/index.vm
-
-
- #if #else if #else 條件判斷
-
#if($foo) Velocity #end
#if中的關係和邏輯符號:
<、<=、==、>=、>、$$、||、!
-
-
- #foreach 條件循環
-
#foreach($product in $allProducts) $product #end
$allProducts:變量的內容,Vector,Hashtable、ArrayList
$product:被顯示的值
默認的循環次數引用變量名爲#velocityCount,可以在配置文件velocity.properties中修改你想要的:
directive.foreach.counter.name = velocityCount
directive.foreach.counter.initial.value = 1
#可以對所有可循環次數加一個最大值來控制,默認的是-1,表示無限制
directive.foreach.maxloops = -1
-
-
- #include 引入本地文件
-
- 導入的文件內容不會被模板引擎解析
- 出於安全考慮,導入文件引該放到TEMPLATE_ROOT目錄下
- 一次可以導入多個文件,文件名之間用逗號隔開
- 通常使用變量引用替代文件名
#include("aaa.txt",aata)
-
-
- #parse 引入其他模板
-
模板引擎會解析導入的模板引擎的VTL語句。
#parse("parse.vm")
-
-
- #stop 停止模板引擎的執行並返回,在Debug時很有用
- #macro VTL的宏指令
-
- 允許定義一段重複使用的VTL模板(Velocimacros)
- 默認情況下,宏保存在VM_global_library.vm文件中
- Velocimacros可以有0或多個參數
- ##創建VTL宏showTree #macro(showTree) #if($treeList) #foreach($e in $value) $e #end #end #end ##調用 #showTree() ##創建VTL帶參數的宏 #macro(showList $val) #if($value) #foreach($e in $val) $e #end #end #end ##調用 $showList($treeList)
- VTL的實際應用
- 初始化Velocity
- 創建Context對象
- 添加數據到Context
- 選擇模板合併模板和數據產生輸出頁面
public static void main(String args) throws IOException { //1)初始化 Velocity.init(); //2)創建Context VelocityContext velocityContext = new VelocityContext(); //3)添加數據到Context velocityContext.put("name","Velocity"); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out)); Template template = Velocity.getTemplate("src/com/me/velocity/index.vm"); //4)合併模板和數據輸出到頁面 template.merge(velocityContext,bufferedWriter); bufferedWriter.flush(); bufferedWriter.close(); }
index.vm
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <!-- 頁面對變量賦值 --> #set( $current = "Velocity22") <!-- 輸出 --> $current is great! <br/><br> <!-- 輸出後臺context設置的參數 --> $name <br/> <!--String循環--> #foreach( $elem in $arrList) $elem</br> #end <!--對象循環--> #foreach( $elem in $userList) 名字:$elem.name 性別:$elem.sex 地址:$elem.address</br> #end hello $name </body> </html>
- Velocity與MVC架構的關係
- Velocity與JSP
與JSP相比,作爲一種完全自包含的模板引擎和腳本解釋器,Velocity擁有完全封閉的模型。任何針對系統或Java編程語言的訪問都必須明確地啓用。默認情況下,Velocity模板不能訪問Java編程語言的任何方面。這種封閉的模型使Velocity能夠提供分離的模板表示層,與任何應用程序業務邏輯或者數據管理代碼清晰地劃分開,實現真正的MVC。
-
- Velocity與Struts
對於Velocity在View層的實際應用,只要在Struts-config.XML裏配置好後,就可以在ACTION中直接將其mapping.forward(“success”)即可。
<action path=”/logoff”
type=”org.apache.struts.webapp.example.LOgoffAction”>
<forward name=”success” path=”/index.vm”>
</action>
- 靜態引用輸出
Velocity遇到一個不能處理的引用時,一般他會直接輸出這個引用$email的寫法,頁面上會看到的是$email,如下例,我們可以在$後面加上一個!號,那麼就會輸出空白:
- 方法
附:
velocity官網:http://velocity.apache.org/
velocity語法參考:http://velocity.apache.org/engine/devel/vtl-reference.html
源代碼參考:https://github.com/xujijun/my-spring-boot