velocity语法小结
生活随笔
收集整理的這篇文章主要介紹了
velocity语法小结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 注釋
- 2. 定義變量
- 3. 條件語句
- 4. 循環標簽
- 5. Velocity 中的宏
- 6. debug語法標簽
- 7. 不存在的變量顯示為空白
- 8. 本地文件引入
- 9. 調用類方法
1. 注釋
單行注釋 :
## 內容多行注釋:
#* 內容 *#文檔注釋:
#**內容*#2. 定義變量
可以使用#set 來定義變量 ,舉個例子:
① 模板內容:
#set($word="zhuoqianmingyue") ${word}#$word #set($surname="Lee") #set($name="junkui") #set($fullname="$surname $name") ${fullname}② 單元測試方法:
public void set() {String templatePath = "cn/lijunkui/api/set.vm";String htmlPath = "D:\\set.html";test(templatePath,htmlPath); }③ 測試類
private void test(String templatePath, String htmlPath) {VelocityEngine ve=new VelocityEngine();//設置模板加載路徑,這里設置的是class下 ve.setProperty(Velocity.RESOURCE_LOADER, "class");ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");try { //進行初始化操作 ve.init(); //加載模板,設定模板編碼Template t=ve.getTemplate(templatePath,"utf-8");//設置初始化數據 VelocityContext context = new VelocityContext(); //設置輸出 PrintWriter writer = new PrintWriter(htmlPath); //將環境數據轉化輸出 t.merge(context, writer); writer.close(); } catch (Exception e) { e.printStackTrace(); } }3. 條件語句
語法:
#if #elseif #else #end下面為模板內容示例:
#set($number = 1)#if($number == 1)這個數是 1 #elseif($number == 2)這個數是 2 #else這個數是3 #end4. 循環標簽
循環標簽可以使用#foreach
舉例:
#foreach ( $item in [1..5] )$item #end-------------------------------#set ( $arr = [0..1] ) #foreach ( $x in $arr )$x #end ------------------------------- #set ( $arr2 = [0..1] ) #set ( $k = 1 ) #foreach ( $x in $arr2 )x:$x,k: $k #set($k = $k+1) #end -------------------------------5. Velocity 中的宏
舉例:
#macro(html)<h2>這是一個宏</h2> #end #macro(htmlContent $content)<h2>$content</h2> #end #html() #htmlContent("有參數的宏")6. debug語法標簽
debug語法標簽可以使用#stop,舉例:
stop前的內容 #stop stop后的內容7. 不存在的變量顯示為空白
為了把不存在的變量或變量值為null的對象$msg顯示為空白,則只需要在變量名前加一個“!”號即可,舉例:
#set($word="zhuoqianmingyue") 存在的內容:${word}# 不存在的內容:${word1} 不存在的內容不顯示:$!{word1} <br>---------------------------<br> #if($!{word1})存在#else不存在 #end8. 本地文件引入
- #include : 可以引入多個文件,被引入文件的內容將不會通過模板引擎解析
- #parse: 只能引入單個文件,引入的文件內容Velocity將解析其中的velocity語法并移交給模板
舉例:
#include( "abc.txt","cde.txt" ) #parse("parent.vm")9. 調用類方法
舉例:
① 定義一個String工具類 將小寫內容轉換為大寫:
package cn.lijunkui.api;public class StringUtils {public static String toUpper(String word) {return word.toUpperCase();} }② 編寫模板引擎初始化并將工具類的實例對象加載進模板的上下文中:
public void initWihtFun(String templatePath, String htmlPath) {VelocityEngine ve=new VelocityEngine();//設置模板加載路徑,這里設置的是class下 ve.setProperty(Velocity.RESOURCE_LOADER, "class");ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "D:\\workspace-sts-3.9.5.RELEASE\\velocitylearn\\target\\velocitylearn\\WEB-INF\\classes\\cn\\lijunkui\\api"); try { //進行初始化操作 ve.init(); //加載模板,設定模板編碼Template t=ve.getTemplate(templatePath,"utf-8");//設置初始化數據 VelocityContext context = new VelocityContext(); StringUtils stringUtils = new StringUtils();context.put("stringUtils", stringUtils);//設置輸出 PrintWriter writer = new PrintWriter(htmlPath); //將環境數據轉化輸出 t.merge(context, writer); writer.close(); } catch (Exception e) { e.printStackTrace(); } }③ 編寫模板內容:
#set($word="zhuoqianmingyue") $stringUtils.toUpper("abc") $stringUtils.toUpper(${word})④ 測試用例:
public void useFun() {String templatePath = "cn/lijunkui/api/useFun.vm";String htmlPath = "D:\\useFun.html";initWihtFun(templatePath,htmlPath); }總結
以上是生活随笔為你收集整理的velocity语法小结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【SQL语句】MySql、SqlServ
- 下一篇: SpringBoot 记录操作日志