隨筆 - 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 閱讀(245) 評論(0)  編輯  收藏 所屬分類: Velocity

          主站蜘蛛池模板: 奇台县| 定边县| 甘南县| 渝北区| 绥宁县| 沭阳县| 临武县| 东辽县| 隆回县| 阿勒泰市| 嫩江县| 郧西县| 眉山市| 冷水江市| 若尔盖县| 防城港市| 巴东县| 双牌县| 杭锦后旗| 尉犁县| 淅川县| 闵行区| 兰溪市| 通江县| 天台县| 区。| 宁化县| 赫章县| 长岭县| 河池市| 上虞市| 泸溪县| 巴里| 渝中区| 龙井市| 南汇区| 西平县| 常德市| 长葛市| 黑河市| 三河市|