<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    數據加載中……
    velocity 例子
    最簡單的hello world 程序。
    基礎搭建:從網上下載velocity.zip,把lib下的.jar文件導入到工程中。另外還要導入velocity.jar 和velocity-dep.jar.
    hellosite.vm  放到工程的目錄底下。
    Hello $name! Welcome to $site world!
    HelloWorld.java
    package com;
    import java.io.StringWriter;
    import org.apache.velocity.Template;
    import org.apache.velocity.VelocityContext;
    import org.apache.velocity.app.VelocityEngine;
    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""DingDangXiaoMa");
            context.put(
    "site""http://www.sun.com");
            
    /* now render the template into a StringWriter */
            StringWriter writer 
    = new StringWriter();
            t.merge(context, writer);
            
    /* show the World */
            System.out.println(writer.toString());
        }
    }
    直接運行這個程序,就可以輸入模板中的信息。
    2。web程序,servlet 調用vm顯示信息。
    hello.vm 放在webRoot/web/下。
    <html>
        
    <body >        
         #set($name
    ="Hello ! DingDang")
            Hello, $name 
            woowoo
        
    </body>
    </html>
    servlet 文件:
    public class SampleServlet extends VelocityServlet {
        
    protected Properties loadConfiguration(ServletConfig config)
                
    throws IOException, FileNotFoundException {
            Properties p 
    = new Properties();
            String path 
    = config.getServletContext().getRealPath("/");
            
    if (path == null) {
                System.out
                        .println(
    " SampleServlet.loadConfiguration() : unable to "
                                
    + "get the current webapp root.  Using '/'. Please fix.");
                path 
    = "/";
            }
            p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
            p.setProperty(
    "runtime.log", path + "velocity.log");
            
    return p;
        }
        
    public Template handleRequest(HttpServletRequest request,
                HttpServletResponse response, Context ctx) {
            
    /**
             * 我的一些測試輸出
             
    */
            System.out.println(
    "Hello  everyone");
            System.out.println(
    "sfdsfsd");
            System.out.println(ctx);
            System.out.println(ctx.toString());
            System.out.println(request.getContextPath());
            Template template 
    = null;

            
    try {
                
    /**
                 * 主要代碼
                 
    */
                Velocity.init();
                
    /**
                 * 注意相對路徑
                 
    */
                template 
    = Velocity.getTemplate("/web/hello.vm");
                VelocityContext context 
    = new VelocityContext();
                context.put(
    "name""wowowowowowo  hello   hello  hello");
                
    // template = Velocity.getTemplate("/web/sample.html");
            } catch (Exception e) {
                e.printStackTrace();
            }
            
    return template;
        }
    }
    以上就是兩個最簡單的velocity的例子。 一個是java application 一個是java web程序。
    參考資料:
    http://www.javaresearch.org/article/38895.htm

    posted on 2008-04-15 15:01 叮當小馬 閱讀(4906) 評論(3)  編輯  收藏 所屬分類: Velocity

    評論

    # re: velocity 例子 2008-04-15 15:27 叮當小馬

    java application 還有一種寫法。
    public static void main(String[] args) throws Exception {

    Velocity.init();

    VelocityContext context = new VelocityContext();

    context.put("name", "yy");

    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    Template template = Velocity.getTemplate("hello.vm");

    template.merge(context, writer);

    writer.flush();

    writer.close();

    }
      回復  更多評論    

    # re: velocity 例子 2011-07-08 11:17 uo;

    o09;i
      回復  更多評論    

    # re: velocity 例子 2011-07-08 11:17 uo;

    ukrkrkrkr
      回復  更多評論    

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲熟妇AV一区二区三区浪潮 | 免费观看黄色的网站| 青青青国产手机频在线免费观看 | 成在线人直播免费视频| 无人视频免费观看免费视频| 女人隐私秘视频黄www免费| 91精品全国免费观看含羞草| 日韩a在线观看免费观看| 亚洲国产精品人人做人人爱| 久久久久亚洲精品影视| 亚洲人色大成年网站在线观看| 偷自拍亚洲视频在线观看| 久99久精品免费视频热77| 免费无码又爽又刺激毛片| 亚洲中文久久精品无码ww16| 亚洲毛片基地4455ww| 中文字幕a∨在线乱码免费看| 黄页网站免费在线观看| 亚洲中文字幕无码日韩| 久久精品成人免费看| 亚洲综合国产精品第一页| 亚洲乱人伦中文字幕无码| 国产精品久久免费| 亚洲午夜福利在线观看| 国产91成人精品亚洲精品| 亚洲乳大丰满中文字幕| 亚洲一区二区免费视频| 久久久久久亚洲av无码蜜芽 | 任你躁在线精品免费| 亚洲中久无码不卡永久在线观看| 99免费精品视频| 亚洲一级毛片中文字幕| 永久看日本大片免费35分钟| 国产亚洲色婷婷久久99精品| 一区二区三区精品高清视频免费在线播放| 亚洲热妇无码AV在线播放| 99久久国产免费-99久久国产免费| 亚洲综合av永久无码精品一区二区| 一级毛片不卡片免费观看| 亚洲a∨无码精品色午夜| 99人中文字幕亚洲区|