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

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

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

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

            任何 Velocity 的應(yīng)用都包括兩個方面:
            第一是: 模板制作,在我們這個例子中就是 hellosite.vm:
            它的內(nèi)容如下(雖然不是以 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() );
          }
          }

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

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

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

          出處:牧羊人手記

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

          主站蜘蛛池模板: 天津市| 东辽县| 响水县| 东台市| 潞城市| 吉木萨尔县| 囊谦县| 会泽县| 平原县| 高州市| 潞城市| 庐江县| 杭州市| 柘城县| 井冈山市| 久治县| 布尔津县| 比如县| 贞丰县| 盐源县| 五指山市| 武陟县| 土默特右旗| 彭山县| 章丘市| 时尚| 那曲县| 绥宁县| 丹东市| 察雅县| 大埔县| 杭锦后旗| 秦皇岛市| 易门县| 丹东市| 竹溪县| 新宾| 阿瓦提县| 阜宁县| 兴和县| 崇仁县|