Velocity研究學習文檔

來源:互聯網
上載者:User

1、概述:
a) Velocity是 一個基於java的模板引擎,簡稱VTL。
b) 主要目的是彌補JSP的不足,把頁 面設計從繁重的java編碼中解脫出來。
c) 使用新穎的文法格式,簡潔、高效。
2、基本概念
a) Velocity的 文法雖然不完全類似於java但也基於物件導向的文法規範。
b) 基礎語言元素和控制語句如附錄A。
i. ‘#’所有的Velocity語句都是由它開始,結尾不需要特殊符號;
例如:
#if( $foo == $bar )
#if( $foo > 42 )
#if( $foo < 42 )
ii. ‘$’所有的變數,屬性,對象的使用都有它開始;
例如:
$mud-Slinger_9
$!mud-Slinger_9
${mud-Slinger_9}
iii. 變數;
1. Regex:
$ [ ! ][ { }[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ ] ]
例如:
Normal notation: $mud-Slinger_9
Silent notation: $!mud-Slinger_9
Formal notation: ${mud-Slinger_9}
2. 變數分為三種基本形式:
普通型(Normal notation),這是Velocity中最常用的變 量類型,可適用於絕大多數情況;
啞元型(Silent notation),主要用在當你使用一個變數,可是它 還沒有值時,啞元型會 為你輸出一個Null 字元串;
正式型(Formal notation), 這種類型主要用載需要區分變數名稱時,如使用變數$test但在頁面中後面緊跟著頁面內容,如:$testHello,變數會被誤認為 是$testHello,所以改變如下:${test}Hello,即可正常使用;
3. 變數的注釋:
在Velocity中‘$’是保 留字,所以頁面中對它的使用就會受到一定的限制,如下的頁面內容:
$2003year,是不會有問題的,因為在Velocity中變數必須使用字 母開頭(’!’、’{‘除外}所以這個字串會被正常顯示;
$year2003,出現這樣的字串,分兩種情況:1.但當前並沒有定義這個變數名,系 統一樣會正常顯示;換言之如果要使用變數但沒有 定義系統都會將其當作一般字串列印出來;2. 當前已定義這個變數名,則系統會把所有出現的此字串當作變數處理,為避免這種情況,Velocity給出了特殊符號修飾符’\’,假設給此變數賦值 good year,結果如下:
$year2003 good year
\$year2003 $year2003
\\$year2003 \good year
\\\$year2003 \$year2003
以此類推。
註:單獨使用變數,如:<input type="text" name="email" value="$email"/>,不算完整語句,不需要在行首加’#’
iv. 屬 性:
$ [ { }[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ ] ]
3、例子
a) 一個參數設定的例子,
<html>
<body>HELLO $CUSTOMERNAME</body>
</html>
我們可以在運行時得到客戶的名字,然後把它插入到這個模版中替換變數$CUSTOMERNAME,整個替換過程是由Velocity進行控制的,而且java的調用代碼也非常簡單,如我們可以在java代碼中這樣 調用
/***********************************************************/
//這個檔案中設定了Velocity使 用的log4j的配置和Velocity的模版檔案所在的目錄
Velocity.init("D:\\Template\\resource\\jt.properties");
//模版檔案名稱,模版檔案所在的路徑在上一條語句中已經設定了
Template template = Velocity.getTemplate("hello.vm", "gb2312");
//執行個體化一個Context
VelocityContext context = new VelocityContext();
//把模版變數的值設定到context中
context.put("CUSTOMERNAME", "My First Template Engine ---- Velocity.");
//開始模版的替換
template.merge(context, writer);
//寫到檔案中
PrintWriter filewriter = new PrintWriter(new FileOutputStream(outpath),true);
filewriter.println(writer.toString());
filewriter.close();
b) 操作對象的一個例子
<html>
<body>
Name:$Customer.Name()
Address:$Customer.Address()
Age:$Customer.Age()
</body>
</html>

java的代碼:
/***********************************************************/
//設定客戶資訊
Customer mycustomer = new Customer();
mycustomer.setName("Velocity");
mycustomer.setAddress("jakarta.apache.org/velocity/index.html");
mycustomer.setAge(2);
//這個檔案中設定了 Velocity 使 用的 Log4j 的配置和Velocity的模 版檔案所在的目錄Velocity.init("D:\\Template\\resource\\jt.properties");
//模版檔案名稱,模版檔案所在的路徑在上一條語句中已經設定了
Template template = Velocity.getTemplate("hello.vm", "gb2312");
//執行個體化一個Context
VelocityContext context = new VelocityContext();
//把模版變數的值設定到context中
context.put("Customer", mycustomer);
//開始模版的替換
template.merge(context, writer);
//寫到檔案中
PrintWriter filewriter = new PrintWriter(new FileOutputStream(outpath),true);
filewriter.println(writer.toString());
filewriter.close();
輸出結果:
<html>
<body>
Name:Velocity
Address:jakarta.apache.org/velocity/index.html
Age:2
</body>
</html>
4、與JSP的比較
Velocity vs. JSP: 兩者都是實現同一功能的template languages. JSP 是 Sun 的標準,Velocity是 Open Source 開發的系統。在Open Source 看 來,JSP 是 Sun 開發的一個"不好用"的產品。"不好用"主要體現在: 難於Debug; 沒有必要地把簡單問題複雜化, 如JSP的先編譯再運行;不 能防止使用者將Java code 嵌入JSP, 從而破壞MVC的設計. 並且Sun 將JSP冠之以"Standard",有“脅天子以令諸侯”之嫌。當然,Struts和Tag Lib將Java code 從JSP中剝離,挽救了JSP.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.