日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Velocity-初體驗(yàn)

系統(tǒng) 2722 0

Velocity是什么

Velocity是一個(gè)基于java的模板引擎,類似與Freemarker。

為啥要用模板引擎

  • 模板引擎的速度要比jsp快,因?yàn)镴SP需要編譯成servlet。
  • 將數(shù)據(jù)(Date)和模板(View)分離,讓不同的角色只關(guān)注自己的部分。

Velocity和Freemarker的比較

  • 功能上:FreeMarker的功能比Velocity多,是Velocity的超集。
  • 使用上:Velocity更加簡(jiǎn)單和輕量級(jí)。

Velocity Template Language (VTL)

基本原則 :使用$字符開始的用于得到什么;使用#字符開始的用于作些什么。如下
a) 變量:#set( $foo = "gibbous" ) $moon = $foo
b) 屬性:$customer.Address
c) 方法:$customer.getAddress()
注意感嘆號(hào)!:用來(lái)強(qiáng)制把不存在的變量顯示為空白,所以強(qiáng)烈建議加上!,如$!message。在Freemarker里也建議這樣。
其他的在使用中可以參考文檔 Velocity學(xué)習(xí)指南.doc

Velocity的工作原理

三步了解Velocity的大致原理。

?
              import java.io.StringWriter; 
import org.apache.velocity.VelocityContext; 
import org.apache.velocity.Template; 
import org.apache.velocity.app.Velocity; 
import org.apache.velocity.exception.ResourceNotFoundException; 
import org.apache.velocity.exception.ParseErrorException; 
import org.apache.velocity.exception.MethodInvocationException; 
  
Velocity.init(); 
//1:構(gòu)建一個(gè)上下文,用來(lái)存放數(shù)據(jù)(date)。 
VelocityContext context = new VelocityContext(); 
context.put( "name", new String("Velocity") ); 
Template template = null; 
try
{ 
   //2:讀取模板(View) 
   template = Velocity.getTemplate("mytemplate.vm"); 
} 
catch( ResourceNotFoundException rnfe ) 
{ 
   // couldn't find the template 
} 
catch( ParseErrorException pee ) 
{ 
  // syntax error: problem parsing the template 
} 
catch( MethodInvocationException mie ) 
{ 
  // something invoked in the template 
  // threw an exception 
} 
catch( Exception e ) 
{} 
  
StringWriter sw = new StringWriter(); 
//3:組合(control)數(shù)據(jù)和模板,最后寫到sw里,sw即最終數(shù)據(jù)。 
template.merge( context, sw ); 


            
??

我覺(jué)得Velocity是由上至下逐行掃描模板,當(dāng)掃描到占位符(如$和#)的時(shí)候,通過(guò)JAVA的反射機(jī)制(或者cglib直接生成字節(jié)碼)調(diào)用JAVA的屬性和方法,生成最終數(shù)據(jù)。

學(xué)習(xí)資料

官方網(wǎng)址: http://velocity.apache.org/engine/releases/velocity-1.7/

Velocity-初體驗(yàn)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 阿克| 平罗县| 左贡县| 玉龙| 宁国市| 湘阴县| 皋兰县| 仙居县| 渝北区| 宁化县| 京山县| 长寿区| 东丰县| 花莲县| 开平市| 阆中市| 曲阜市| 阳曲县| 清水县| 南川市| SHOW| 乐平市| 新丰县| 马公市| 鄂托克前旗| 灌阳县| 敦煌市| 商南县| 临泉县| 伽师县| 东乌珠穆沁旗| 磐安县| 玉树县| 永顺县| 长治市| 三原县| 池州市| 莒南县| 广水市| 克什克腾旗| 汾西县|