velocity 在netiler常用指令及應用
1.Velocity使用示例
1.1.基本用法
l純html用法
<div> Hello World </div> |
l#set賦值與取值
賦值#set($word="hello") 取值 $word 推薦寫成${word} 取值的另一種寫法 $!word $word和 $!word區別是當變量word爲null時,$word會在前端顯示字符串"$word",而$!word不顯示任何東西 |
l#foreach()循環
#foreach($stu in $stulist) $i,$stu.name,$stu.sex,$stu.age #end $stulist爲後臺傳來的Student對象集合 #foreach() 結合 #if() #break()用法示例 #foreach($item in $list) $item #if($item=="1") #break() #end #end |
l#include()包含
#include('path') 加載其他.vm文件,文件加載支持相對路徑的模式。 |
1.2.擴展指令
l添加px支持
#px() |
l引入js腳本,css樣式,圖像文件
#script('student') 引入/web/resource/script/student.js #javascript() <script> alert('abc'); 等同於 alert('abc'); #end </script> #style('student') 將加載web/resource/theme/style.css文件,如果文件不存在,頁面將不載入。 #image('student') 將加載web/resource/image/image.jpg文件,等同於HTML<img/>標籤。 |
l獲取路徑
#path('') 的值爲project.xml的namespace的值並以/開頭,/結尾 輸出/demo/ #path('index.nsp') 值爲/demo/index.nsp |
l設置html文檔標題
#title('hello velocity') 頁面源代碼 |
l輸出日期
#date('2013-04-01') 輸出2013-04-01 格式化 #date('2013-04-01','yyyy年M月d日') 輸出2013年4月1日 |
l使用#bean指令調用java對象
#bean('com.leadal.demo.service.DemoService',$bean) ,將加載Spring中的com.leadal.demo.service.DemoService對象,並賦值變量$bean。提供下面程序訪問。 |
l添加按鈕
#btn(‘student’) 使用#btn()時框架默認引入px支持 添加/web/resource/btn/student.btn中所有按鈕 #btn(‘student’,[‘add’,’edit’,’delete’]) 添加/web/resource/btn/student.btn中add,edit,delete三個按鈕 |