隨筆 - 3, 文章 - 152, 評論 - 17, 引用 - 0
          數據加載中……

          velocity 的介紹-------from matrix jdyao

          Velocity 是如何工作的呢? 雖然大多 Velocity 的應用都是基于 Servlet 的網頁制作。但是為了說明 Velocity 的使用,我決定采用更通用的 Java application 來說明它的工作原理。

            似乎所有語言教學的開頭都是采用 HelloWorld 來作為第一個程序的示例。這里也不例外。

            任何 Velocity 的應用都包括兩個方面:
            第一是: 模板制作,在我們這個例子中就是 hellosite.vm:
            它的內容如下(雖然不是以 HTML 為主,但是這很容易改成一個 html 的頁面)

          Hello $name! Welcome to $site world!

            第二是 Java 程序部分:
            下面是 Java 代碼

          import java.io.StringWriter;
          import org.apache.velocity.app.VelocityEngine;
          import org.apache.velocity.Template;
          import org.apache.velocity.VelocityContext;

          public class HelloWorld
          {
          public static void main( String[] args )
          throws Exception
          {
          /* first, get and initialize an engine */

          VelocityEngine ve = new VelocityEngine();
          ve.init();

          /* next, get the Template */

          Template t = ve.getTemplate( "hellosite.vm" );

          /* create a context and add data */

          VelocityContext context = new VelocityContext();

          context.put("name", "Eiffel Qiu");

          context.put("site", "http://www.eiffelqiu.com");

          /* now render the template into a StringWriter */

          StringWriter writer = new StringWriter();

          t.merge( context, writer );

          /* show the World */

          System.out.println( writer.toString() );
          }
          }

          將兩個文件放在同一個目錄下,編譯運行,結果是:

          Hello Eiffel Qiu! Welcome to http://www.eiffelqiu.com world

            為了保證運行順利,請從 Velocity 的網站 http://jakarta.apache.org/velocity/ 上下載 Velocity 的運行包,將其中的 Velocity Jar 包的路徑放在系統的 Classpath 中,這樣就可以編譯和運行以上的程序了。

          出處:牧羊人手記

          posted on 2005-07-30 22:26 閱讀(239) 評論(0)  編輯  收藏 所屬分類: Velocity

          主站蜘蛛池模板: 太谷县| 北安市| 嘉荫县| 申扎县| 贺州市| 娱乐| 将乐县| 桂林市| 宕昌县| 忻州市| 新乡县| 来宾市| 崇阳县| 广州市| 乐亭县| 涿州市| 杂多县| 城口县| 洞口县| 德保县| 嘉荫县| 花莲县| 金塔县| 曲靖市| 遂川县| 宽甸| 九江县| 托里县| 吉安市| 叙永县| 承德县| 安义县| 自治县| 无为县| 贵阳市| 鄂温| 旬阳县| 富川| 若羌县| 永济市| 大名县|