Java模板擎Velocity模板引擎的介紹和基本的模板語言語法使用

jopen 10年前發布 | 32K 次閱讀 Velocity 模板引擎

類似于PHP中的Smarty,Velocity是一個基于java的模板引擎(template engine)。它允許任何人僅僅簡單的使用模板語言(template language)來引用由java代碼定義的對象。從而實現界面和Java代碼的分離,使得界面設計人員可以和java程序開發人員同步開發一個遵循 MVC架構的web站點。

另外,Velocity的能力遠不止web站點開發這個領域,例如,它可以從模板(template)產生SQL和PostScript、XML,它也可以被當作一個獨立工具來產生源代碼和報告,或者作為其他系統的集成組件使用。 Velocity也可以為Turbine web開發架構提供模板服務(template service)。Velocity+Turbine提供一個模板服務的方式允許一個web應用以一個真正的MVC模型進行開發。

編寫Velocity版的Hello World

獲取Velocity相關JAR文件:

從http://velocity.apache.org/網站上下載最新的Velocity,這里我們下載了velocity-1.7.zip

相關Jar包添加到項目中:

解壓velocity-1.7.zip,發下其根目錄下有兩個JAR文件:

velocity-1.7.jar velocity-1.7-dep.jar

其中velocity-1.7-dep.jar包含了:

velocity-1.7.jar commons-collections-3.2.1.jar commons-lang-2.4.jar oro-2.0.8.jar(這些JAR文件位于解壓目錄的lib目錄下)

在JAR包不沖突的情況下可以直接使用velocity-1.7-dep.jar

載類路徑下添加velocity.properties文件:

該文件一般包含如下配置:

runtime.log = F:\project\MusicalInstrumentsStore\velocity_example.log
file.resource.loader.path = F:\project\MusicalInstrumentsStore\vm
input.encoding = UTF-8
output.encoding = UTF-8

runtime.log指定日志文件存放位置
file.resource.loader.path指定模板的加載位置
input.encoding指定輸入編碼
output.encoding指定輸出編碼

在Java文件中初始化Velocity模板引擎并設置Velocity上下文的一些變量然后把生成的模板輸出到StringWriter:

//初始化模板引擎
Velocity.init("src/velocity.properties");
//獲取VelocityContext
VelocityContext context = new VelocityContext();
//為Context設置變量
context.put("title", "HelloWorld");
context.put("author", "arthinking");
//獲取模板文件
Template template = Velocity.getTemplate("helloworld.vm");
StringWriter sw = new StringWriter();
//使用模板文件的merge函數合并模板和context提供的變量,輸出到StringWriter中
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());

編寫helloworld.vm模板文件(保存在file.resource.loader.path設置的目錄下):

${who}
${content}

運行Java文件,使用Velocity生成的信息就打印出來了。

注:如果who為空時,${who}會原樣輸出,為了使之不輸出,可以在$后加個!:$!{who}

Velocity模板語言基本語法

訪問對象屬性:

和使用EL表達式差不多,直接使用”.”導航。
如訪問object對象的id屬性:${object.id }

遍歷List集合:

#foreach($element in $list)

#element

end</pre></div>

使用判斷語句:

#if($condition)
    true

else

false

end</pre></div>

獲取迭代索引值:

默認使用變量名:$velocityCount
也可以自定義此變量名,在velocity.properties中設置:

directive.foreach.counter.name=index

設置索引起始位置為0:

directive.foreach.counter.initial.value=0

遍歷Map變量:

#foreach($key in $map.keySet())
    $key : $map.get($key)

end</pre></div>

在模板中進行賦值:

#set(#a=”Hello World!”)
$a

set($array1=[1..10])

foreach($entry in $array1)

#entry

end</pre></div>

使用Velocity模板引擎生成文件:

//初始化模板引擎
Velocity.init("src/velocity.properties");
//獲取VelocityContext
VelocityContext context = new VelocityContext();
//為Context設置變量
context.put("content", "HelloWorld");
context.put("who", "arthinking");
//獲取模板文件
Template template = Velocity.getTemplate("helloworld.vm");
//創建輸出文件
File output = new File("D:/Velocity/1.html");
if(!output.getParentFile().exists())
    output.getParentFile().mkdir();
//創建輸出流
FileOutputStream outputStream = new FileOutputStream(output);
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
template.merge(context, bufferedWriter);

bufferedWriter.flush(); outputStream.close(); bufferedWriter.close();</pre></div> </div> 本文鏈接:http://www.itzhai.com/the-introduction-of-the-velocity-template-engine-template-language-syntax-and-basic-use.html

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!